I let the user take a foto with the camera and then save the the Bitmap to the gallery like this:
private void letUserTakeUserTakePicture() {
Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(getTempFile(getActivity())));
String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
chooserIntent.putExtra
(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePhotoIntent});
startActivityForResult(chooserIntent, Values.REQ_CODE_TAKEPICTURE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final File file = getTempFile(getActivity());
Bitmap captureBmp = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), Uri.fromFile(file));
Uri uri = addImageToGallery(getActivity(), captureBmp, "herp", "derp"));
}
private File getTempFile(Context context) {
if (tempFile != null) {
return tempFile;
}
final File path = new File(Environment.getExternalStorageDirectory(), context.getPackageName());
if (!path.exists()) {
path.mkdir();
}
tempFile = new File(path, "image.tmp");
return tempFile;
}
private Uri addImageToGallery(Context context, Bitmap bitmap, String title, String description) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, title);
values.put(MediaStore.Images.Media.DESCRIPTION, description);
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), bitmap, title, description);
return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
The image is saved to the gallery just fine. I receive an Uri that looks like this:
content://media/external/images/media/125
but when I pass this URI to an instance of UniversalImageLoader it can't find the image (java.io.FileNotFoundException) although I knopw for sure UIL can handle this kind of Uris (works just fine if I let the user chose an image from the gallery).
I'm confused on how to get a hold of the image I saved to the gallery so I can display it and I'm not at a point where I get confused more the more I try.
Any help appreciated, what am I doing wrong?
EDIT: Pr38y's comment is right, the error was in the addImageToGallery() method where I am trying to insert the same imange twice.
Thanks!