Get file path of image on Android
115,988
Solution 1
Posting to Twitter needs the image's actual path on the device to be sent in the request to post. I was finding it mighty difficult to get the actual path and more often than not I would get the wrong path.
To counter that, once you have a Bitmap
, I use that to get the URI from using the getImageUri()
. Subsequently, I use the tempUri
Uri instance to get the actual path as it is on the device.
This is production code and naturally tested. ;-)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
knop.setVisibility(Button.VISIBLE);
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
System.out.println(mImageCaptureUri);
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
Solution 2
Try out with mImageCaptureUri.getPath();
By Below Way :
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
//Get your Image Path
String Path=mImageCaptureUri.getPath();
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
knop.setVisibility(Button.VISIBLE);
System.out.println(mImageCaptureUri);
}
Related videos on Youtube
Author by
Rick de Jong
Updated on July 16, 2022Comments
-
Rick de Jong almost 2 years
I have an app that can make pictures and upload them. The upload requires the file path of the photo but I can't get it.
This is my code:
public void maakfoto (View v) { Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri); startActivityForResult(cameraIntent, CAMERA_REQUEST); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) { Bitmap photo = (Bitmap) data.getExtras().get("data"); imageView.setImageBitmap(photo); knop.setVisibility(Button.VISIBLE); System.out.println(mImageCaptureUri); } }
Please help me to get the file path.
-
Maikel about 11 yearsstackoverflow.com/questions/9564538/… hope this will help
-
Priyanka Singh almost 4 years@MaikelR Can you please help me with this detailed and Bounty added question? It would be really appreciate: stackoverflow.com/questions/62783444/…
-
-
Rick de Jong about 11 yearsApp will crash when I did this.
-
Bhavesh Patadiya about 11 yearsit might because mImageCaptureUri is not defined and initialized Globally. try to define it globally. and pass this in putextra while setting intent for captureimage.
-
Rick de Jong about 11 yearsI've got "Failing delivering 'Result'.
-
Siddharth Lele about 11 years@RickdeJong: Are you using the code exactly as in the post? Because if you are, it should work.
-
Siddharth Lele about 11 years@RickdeJong: At what line is that error in your
onActivityResult()
? -
Siddharth Lele about 11 yearsAah. You had me scared for a while. I thought if it doesn't work for you, my app must be broken too. ;-)
-
Siddharth Lele about 11 years@RickdeJong: Glad to have been of help. :-)
-
Adrien Cerdan almost 11 years@iceMAN You save my day :)
-
Siddharth Lele about 10 years@Nomi: I am glad it was helpful. :-)
-
Don Larynx almost 9 yearsError:(81, 27) error: cannot find symbol method getImageUri(Context,Bitmap)
-
Siddharth Lele almost 9 years@DonLarynx: Have you also added the method
getImageUri
that is already shown in the answer? -
Siddharth Lele over 8 years@delive: Can you explain a little more about what didn't work? Perhaps, you could post a new question referring to this answer and then link to it in a comment.
-
Admin over 8 yearsYes, your code not work in >5.0 is so easy, your code is 2013, you can't pretend that code work always ....
-
Admin over 8 yearsThis is a correct line : Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);, but I put +1 because I was searching your "getImageUri"
-
Siddharth Lele over 8 years@delive: I haven't pretended about anything at all. I asked you to explain the problem you were facing with the code because Not work for me ! doesn't really help me to help you. Anyway. Thanks for clarifying.
-
ElliotM over 8 yearsThis solution works great, quick question though @IceMAN - I can see that the image taken is high quality from within the gallery, but when I receive the image from the file path, it's very small, thus low quality when trying to blow it up (as the gallery would) is there any way to define the size of image I want in return?
-
Siddharth Lele over 8 years@ElliotM: Not entirely sure why the code in question will return a small image. I am not scaling it down in it. Perhaps you could post your entire code (image picker) in a new question and link to it in a comment. I'll take a look at it.
-
ElliotM over 8 years@IceMAN: I found out that
getData("data")
we only return the thumbnail of the image, see here: stackoverflow.com/questions/6448856/…. I was able to save the image temporarily then grab later on to solve this issue. Thanks anyways. -
Ajit Kumar Dubey over 7 years@SiddharthLele insertImage() creating thumbnail images. Is there any other way to get real picture instead of thumbnail?
-
Oush almost 4 yearsRemember to close the cursor