1

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!

fweigl
  • 21,278
  • 20
  • 114
  • 205
  • take a look at [this SO post](http://stackoverflow.com/a/19874645/3326331) if you are having problem with kitkat o.s gallery intent image uri – Sagar Pilkhwal Sep 29 '14 at 09:38
  • Use `ACTION_PICK` instead of `ACTION_GET_CONTENT`. It will work for all api levels. – Pr38y Sep 29 '14 at 09:43
  • @Pr38y That intent is for letting the user pick an image from the gallery which works fine. My problem is when the user takes a picture with the camera. – fweigl Sep 30 '14 at 06:18
  • You are trying to insert image twice. `Media.insertImage` will save the image and return the url. And `ContentResolver().insert` will do the same thing.but in second case when you are returning uri, `Media.DATA` field is missing. – Pr38y Sep 30 '14 at 11:33
  • You can `return new Uri.parse( MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), bitmap, title, description); )` from `addImageToGallery`. – Pr38y Sep 30 '14 at 11:35

0 Answers0