How to get Image Path just captured from camera

This works for me…

Code:

Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURI(selectedImageUri);

Method: getRealPathFromURI()

//----------------------------------------
    /**
     * This method is used to get real path of file from from uri
     * 
     * @param contentUri
     * @return String
     */
    //----------------------------------------
    public String getRealPathFromURI(Uri contentUri)
    {
        try
        {
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        catch (Exception e)
        {
            return contentUri.getPath();
        }
    }

EDIT:

As I noticed in some device after captured image the data in onActivityResult() is null,

So the alternate way, Pass the specific image filename as a argument to your Intent for capture image as putExtra parameter.

Then also insert this image Uri in Media Store, Now use this Uri for your further use,

You can check whether image is captured or not by File.exist(),

Code looks like,

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image File name");
Uri mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intentPicture,ACTION_TAKE_PICTURE);

Now, you can use the same method for get file path from Uri,

in this case it will be in onActivityResult(),

selectedImagePath = getRealPathFromURI(mCapturedImageURI); // don't use data.getData() as it return null in some device instead  use mCapturedImageUR uri variable statically in your code,

Leave a Comment