Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the Uri of a image stored on the SDCARD?

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

When i want to get the Uri of a image stored on drawable, i use this and it works perfect:

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

But if i want to get the URI of a image stored on the SDCARD, i tryed with this, and it doesn't works, 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 a image stored on the sdcard?

Thanks

like image 700
NullPointerException Avatar asked Nov 28 '11 13:11

NullPointerException


2 Answers

Always use Environment.getExternalStorageDirectory() instead of hard coding the path of external storage directory. Because the path of external storage directory may be different in different API Levels.

Try this:

String fileName = "YourImage.png";
String completePath = Environment.getExternalStorageDirectory() + "/" + fileName;

File file = new File(completePath); 
Uri imageUri = Uri.fromFile(file);
like image 53
SeyedPooya Soofbaf Avatar answered Oct 07 '22 01:10

SeyedPooya Soofbaf


String filename = "image.png";
String path = "/mnt/sdcard/" + filename;
 File f = new File(path);  //  
            Uri imageUri = Uri.fromFile(f);     
like image 24
KK_07k11A0585 Avatar answered Oct 07 '22 03:10

KK_07k11A0585