2

I'm launching camera from my application. I'm using Android.provider.MediaStore.ACTION_IMAGE_CAPTURE with extra android.provider.MediaStore.EXTRA_OUTPUT. If I use EXTRA_OUTPUT or not, photo is added to gallery.

How to get the Uri of the image in Gallery? I haven't found a clean way to do this, and I don't want to use the Uri of last added image to Gallery, because I can not be sure that it is the photo user took.

Edit: Let me add here, that I do know how to get the image. Currently I'm using my Uri which I'm passing with Intent as an EXTRA_OUTPUT. That is not the problem. The problem is that using this method I get two photos written to sdcard, and I prefer that only one is written. Or in case of two is written, one in gallery and one in my own Uri, I would like to use one of those Uri:s and delete the other one. In both of the cases, I need the Uri of the photo, which is saved to Gallery by default.

koskenal
  • 106
  • 1
  • 2
  • 8

3 Answers3

2

If you start the camera by calling

startActivityForResult(cameraIntent, REQUEST_CODE_CAPTURE_IMAGE_ACTIVITY); // start the camera

in onActivityResult you can capture the mediaURI

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == REQUEST_CODE_CAPTURE_IMAGE_ACTIVITY
                && resultCode == RESULT_OK) {
                Uri imageUri = null;
                if (data != null){
                   imageUri = data.getData();
               }
        }
    }

REQUEST_CODE_CAPTURE_IMAGE_ACTIVITY is a static integer value that's used to identify the request that was made through the intent. This is so if you have multiple startActivityForResults you can identify which one is coming back.

Note that if you pre-insert a URI through EXTRA_OUTPUT you will NOT get the URI back through data.getData(); You will have to store the pre-inserted URI in a variable and retrieve it in your onActivityResult while checking to see if imageUri is null..

Ex.

 Uri imageUri = null;
if (data != null){
   imageUri = data.getData();
}
if (imageUri == null && preinsertedUri != null)
 imageUri = preinsertedUri;

// do stuff with imageUri

dymmeh
  • 22,247
  • 5
  • 53
  • 60
  • OK. I tested it without EXTRA_OUTPUT. When I used Sony Xperia, I indeed did get the Uri of photo saved in Gallery. But when I used Samsung Galaxy, data.getData() was null. – koskenal Apr 18 '12 at 06:40
  • 3
    Ok here's what I've found with cameras. Some manufacturers require that you pre-insert a URI for their camera application. Others will return it in data.getData(); It's one of those "wonderful" inconsistencies we tend to see in android across different manufacturers. You should always pre-insert a URI and use EXTRA_OUTPUT..Then in your onActivityResult check to see if data.getData() is null.. if its null, then use the pre-inserted URI as your image URI, otherwise use data.getData() as your URI. – dymmeh Apr 18 '12 at 13:34
  • 1
    'preinsertedUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());' will preinsert a URI. make sure 'preinsertedUri' is a member variable in your application. – dymmeh Apr 18 '12 at 13:38
  • You can use empty ContentValues when inserting the new record into the media store. The values in the database will get filled in when the camera application actually takes the picture – dymmeh Apr 18 '12 at 13:39
  • While old, dymmeh's comment from April 18, 2012 was exactly what I needed to do to get camera image loaded from Kindle Fire HD, and it still worked fine for other devices that worked w/ a null uri. – jpporterVA Nov 20 '13 at 21:38
2

Getting a Uri from data.getData() in onActivityResult() was not a solution. This worked on one out of three test devices, and failed also on Motorola Defy emulator.

So here is what I ended up doing:

  1. Call camera with Android.provider.MediaStore.ACTION_IMAGE_CAPTURE, no EXTRA_OUTPUT
  2. Get the Uri of image that is stored in Gallery

This seems to be working nicely with test devices, but I'm still not completely happy with this solution because I'm not totally ensured that the image added lastly is always the image user captured.

Matthias
  • 7,432
  • 6
  • 55
  • 88
koskenal
  • 106
  • 1
  • 2
  • 8
0

public class SampleImageActivity extends Activity {

private static final int SELECT_PICTURE = 1;

private String selectedImagePath;
private ImageView img;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    img = (ImageView)findViewById(R.id.ImageView01);

    ((Button) findViewById(R.id.Button01))
            .setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
                }
            });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            img.setImageURI(selectedImageUri);
        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

}

udaysagar
  • 11
  • 2