How to get Uri image stored on sdcard?

I need to get the URI of an image stored on an SDCARD.

When I want to get a Uri of an image stored on a drawable, I use this and it works fine:

i.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.image)); 

But if I want to get the URI of an image stored on an SDCARD, I tried with this and it doesn’t work, something is wrong:

 i.putExtra(Intent.EXTRA_STREAM, Uri.parse("/mnt/sdcard/car.jpg")); 

Please can someone tell me how to get the correct uri of the image stored on the SD card?

thanks

+8
android uri android-sdcard
source share
2 answers
 String filename = "image.png"; String path = "/mnt/sdcard/" + filename; File f = new File(path); // Uri imageUri = Uri.fromFile(f); 
+10
source share

Always use Environment.getExternalStorageDirectory() instead of hard coding the path of the external storage directory. Since the path to the external storage file may differ at different levels of the API.

Try the following:

 String fileName = "YourImage.png"; String completePath = Environment.getExternalStorageDirectory() + "/" + fileName; File file = new File(completePath); Uri imageUri = Uri.fromFile(file); 
+12
source share

All Articles