2011-11-28 107 views
8

我需要获取存储在SD卡上的图像的URI。如何获取存储在SD卡上的图像的Uri?

当我想存储在绘制一个图像的URI,我用这个和它的作品完美:

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

但是,如果我想获得存储在SD卡一个图像的URI,我tryed这一点,它不工作,什么是错的:

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

请,可有人告诉我怎么去存储在SD卡一个图像的正确的URI?

感谢

+0

你试过i.putExtra(Intent.EXTRA_STREAM,Uri.parse( “文件://mnt/sdcard/car.jpg”));?让我知道发生了什么.. – user370305

+0

看看这里 http://stackoverflow.com/questions/3873496/how-to-get-image-path-from-images-stored-on-sd-card –

回答

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

始终使用的Environment.getExternalStorageDirectory()而不是硬编码的外部存储目录的路径。因为外部存储目录的路径在不同的API级别可能会有所不同。

试试这个:

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

File file = new File(completePath); 
Uri imageUri = Uri.fromFile(file); 
相关问题