【问题标题】:How to get the Uri of a image stored on the SDCARD?如何获取存储在 SDCARD 上的图像的 Uri?
【发布时间】:2011-11-28 13:03:50
【问题描述】:

我需要获取存储在 SDCARD 上的图像的 URI。

当我想获取存储在 drawable 上的图像的 Uri 时,我使用它并且它工作得很好:

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

但是如果我想获取存储在 SDCARD 上的图像的 URI,我尝试了这个,但它不起作用,是有问题:

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

请问,谁能告诉我如何获取存储在 sdcard 上的图像的正确 uri?

谢谢

【问题讨论】:

标签: android uri android-sdcard


【解决方案1】:

始终使用Environment.getExternalStorageDirectory(),而不是硬编码外部存储目录的路径。因为不同的API Levels中外部存储目录的路径可能不同。

试试这个:

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

File file = new File(completePath); 
Uri imageUri = Uri.fromFile(file);

【讨论】:

    【解决方案2】:
    String filename = "image.png";
    String path = "/mnt/sdcard/" + filename;
     File f = new File(path);  //  
                Uri imageUri = Uri.fromFile(f);     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 2021-10-12
      • 2011-09-29
      • 2012-01-13
      • 1970-01-01
      相关资源
      最近更新 更多