Reputation: 122
I am making an app which has an option to get an image from the camera or from the gallery and display it in an ImageView. I got this from the Android Developers Tutorial. I am using a DialogInterface for this one:
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(ReportIncident.this);
builder.setTitle("Add Picture");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
try {
photoFile = createImageFile();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, 0);
} catch (IOException ex) {
// Error occurred while creating the File
ex.printStackTrace();
}
}
} else if (items[item].equals("Choose from Library")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"), 1);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
createImageFile creates the directory/File where the photo will be saved:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "FIREFLOOD_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
The 'Choose from Library' part is already working but I have a problem with 'Take Photo' part. Specifically on this line:
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
galleryAddPic();
startActivityForResult(takePictureIntent, 0);
Having the putExtra() and galleryAddPic() before the startActivityOnResult() crashes the app after taking the picture but successfully saves the image to gallery. But deleting these two methods from the code displays the photo taken to the ImageView reportImage.
startActivityForResult displays the image taken to the ImageView through activityOnResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1) {
onSelectFromGalleryResult(data);
}else if (requestCode == 0) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
reportImage.setImageBitmap(imageBitmap);
}
}
}
Saving the image to gallery is performed by galleryAddPic:
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
The question is where can I put these two methods (putExtra and galleryAddPic) to make these two funtions work:
- display the photo taken to ImageView
- save the photo to gallery at the same time.
I can't make these two work altogether. Please help. I tried to put the putExtra and galleryAddPic on the onAcitivityResult but it still crashes. galleryAddPic won't work without the putExtra.
Upvotes: 0
Views: 6303
Reputation: 1422
This tutorial will help you try this link http://www.androidhive.info/2013/09/android-working-with-camera-api/
Upvotes: 1