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

穿精又带淫゛_ 提交于 2019-11-30 03:23:47

问题


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


回答1:


String filename = "image.png";
String path = "/mnt/sdcard/" + filename;
 File f = new File(path);  //  
            Uri imageUri = Uri.fromFile(f);     



回答2:


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);


来源:https://stackoverflow.com/questions/8296182/how-to-get-the-uri-of-a-image-stored-on-the-sdcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!