This works for me ...
The 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 on some device, after the captured image, the data in onActivityResult() null ,
So, an alternative way. Pass a specific image file name as an argument for your intention to capture the image as a putExtra parameter.
Then also paste this Uri image into the Media Store, now use this Uri for future reference,
You can check if the image is captured or not using File.exist() ,
The code looks like this:
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 to get the 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,