我正在尝试创建一个具有两个ImageButton活动的应用。单击每个按钮时,相机将打开并拍摄照片。保存图片后,它将在图像按钮内显示为预览。我可以为其中一个做到这一点但是当点击第二个时它会覆盖第一个图像。
有没有办法将点击的元素传递给onActivityResult方法,以便设置哪个ImageButton应该包含图片?
代码如下:
// Activity request codes
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
// directory name to store captured images and videos
private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";
private Uri fileUri; // file url to store image/video
private ImageButton imageButton;
private ImageButton imageButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask_activity_layout);
imageButton =(ImageButton) findViewById(R.id.imageButton);
imageButton2 =(ImageButton) findViewById(R.id.imageButton2);
// Checking camera availability
if (!isDeviceSupportCamera()) {
Toast.makeText(getApplicationContext(),
"Sorry! Your device doesn't support camera",
Toast.LENGTH_LONG).show();
// will close the app if the device does't have camera
finish();
}
}
启动捕获图像的方法:
public void captureImage(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
onActivityResult方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image
// display it in image view
// data.getComponent();
previewCapturedImage(null);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
} else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// video successfully recorded
// preview the recorded video
previewCapturedImage(null);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled recording
Toast.makeText(getApplicationContext(),
"User cancelled video recording", Toast.LENGTH_SHORT)
.show();
} else {
// failed to record video
Toast.makeText(getApplicationContext(),
"Sorry! Failed to record video", Toast.LENGTH_SHORT)
.show();
}
}
}
最后是previewCapturedImage方法:
private void previewCapturedImage(View view) {
try {
// hide video preview
//videoPreview.setVisibility(View.GONE);
imageButton.setVisibility(View.VISIBLE);
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
**imageButton.setImageBitmap(bitmap);**
} catch (NullPointerException e) {
e.printStackTrace();
}
}
我想将从onActivityResult点击的实际ImageButton传递给previewCaptureImage方法,以便在ImageButton或ImageButton2中动态设置图像。
对此的任何帮助都将受到高度赞赏。
由于
Seb
PS:此处发布的代码取自以下示例:http://www.androidhive.info/2013/09/android-working-with-camera-api/
答案 0 :(得分:0)
是。这就是requestCode
函数的startActivityForResult
参数的目的。对于将“接收”图像的每个按钮,您必须使用不同的代码。
类似的东西:
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE_SECONDARY = 101;
private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
根据requestCode
选择将分配捕获位图的ImageView
。