I'm trying the following:
1. Click a button to take a photo.
2. Save photo.
3. Add photo into the gallery.
4. Show photo into an ImageView.
1
and 4
works fine, but I'm having problems with 2
and 3
.
This is my code:
photoFile = createImageFile();
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp;
File imageFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), imageFileName + ".jpg");
mCurrentPhotoPath = imageFile.getAbsolutePath();
return imageFile;
}
With this I have a filePath created where I want to store the image.
Now I call the Intent
with extra params to store the image:
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(getContext(),
"es.antonio.prueba.fileprovider",
photoFile);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePhotoIntent, REQUEST_CAMERA);
}
Now, in my onActivityResult
I call a function to add the photo into the gallery:
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);
getContext().sendBroadcast(mediaScanIntent);
}
And another one to set the photo into the ImageView
:
private void setPic(ImageView mImageView) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
mImageView.setImageBitmap(bitmap);
}
I'm struggling with save the picture taken into the SD because my method is not working (I'm followind Android Developers tutrial). I thought that passing an extra to the Intent should do te the trick but it's not working.
Any hint?