I'm trying to create an app that has an activity with two ImageButtons. When clicking each of the buttons, the camera opens and a picture is taken. once the picture is saved, it is presented as a preview inside the image button. I can do it for one of them but when clicking on the second it overrides the first image.
Is there a way to pass the element that was clicked to the onActivityResult method in order to set which ImageButton should include the picture?
The code is as follows:
// 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();
}
}
The method that starts the capture image:
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);
}
The onActivityResult method:
@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();
}
}
}
Finally the previewCapturedImage method:
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();
}
}
I would like to pass the actual ImageButton clicked from the onActivityResult to the previewCaptureImage method to dynamically set the image either in ImageButton or ImageButton2.
Any help with this will be highly appreciated.
Thanks
Seb
PS: the code posted here was taken from the following example: http://www.androidhive.info/2013/09/android-working-with-camera-api/