我正在尝试开发一个动态创建布局的应用程序。
在我的布局中,我正在创建一个UploadImage
按钮。
我正在另一个 java 类中创建 OnClickListener
,该类实现
`OnClickListener' 类,以便可以在我的应用程序中的不同位置使用它。
但是,不幸的是,我无法启动相机 Activity 。
下面是我的应用程序的源代码。
###MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
GenerateViews genView = new GenerateViews();
genView.addQuestionWithUploadImageOption(this, layout);
}
###activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp" >
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
###GenerateViews.java
public class GenerateViews {
void addQuestionWithUploadImageOption(final Context context,
LinearLayout layout) {
// TODO Auto-generated method stub
LinearLayout layoutImage = new LinearLayout(context);
layoutImage.setOrientation(LinearLayout.HORIZONTAL);
layoutImage.setWeightSum(1);
layoutImage.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
ImageView imageView = new ImageView(context);
imageView.setContentDescription("Image");
imageView.setLayoutParams(new LayoutParams(256, 256));
Button uploadImageButton = new Button(context);
uploadImageButton.setText(R.string.uploadImage);
uploadImageButton.setOnClickListener(new UploadImage());
layoutImage.addView(imageView);
layoutImage.addView(uploadImageButton);
layout.addView(layoutImage);
}
}
从这个GenerateViews
java类中,我调用了UploadImage
类,它实际上实现了OnClickListener。
###UploadImage.java
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class UploadImage extends Activity implements OnClickListener {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
一切正常。我的表单是动态创建的,所有文本等都按我想要的方式显示。
甚至 OnClickListener
也在调用 UploadImage.java
类。
但这是我收到的错误:
接下来我可以尝试什么?
最佳答案
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, REQUEST_CAMERA);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
if (requestCode == REQUEST_CAMERA)
{
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles())
{
if (temp.getName().equals("temp.jpg"))
{
f = temp;
break;
}
}
try
{
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(f.getAbsolutePath(),btmapOptions);
bm = Bitmap.createScaledBitmap(bm, 70, 70, true);
ivRecipeImage.setImageBitmap(bm);
path = android.os.Environment.getExternalStorageDirectory()+ File.separator+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream fOut = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
try
{
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
else if (requestCode == PICK_IMAGE)
{
Uri selectedImageUri = data.getData();
String tempPath = getPath(selectedImageUri, RecipeActivity.this);
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bm = BitmapFactory.decodeFile(tempPath, btmapOptions);
ivRecipeImage.setImageBitmap(bm);
}
}
}
关于android - 从 java 类启动 Camera Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18119062/