【问题标题】:Android how to get the only uri of Media Store Image?Android如何获取Media Store Image的唯一uri?
【发布时间】:2014-09-05 10:56:06
【问题描述】:

我正在尝试获取 uri 并在解析为 MediaStore 图像的路径后。 我这样做是为了获取 uri:

Uri img_uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

使用此函数后,我将路径中的 uri 转换为:

public String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try { 
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
          cursor.close();
        }
    }
}

但结果是我有媒体存储的路径,但有第一个元素。 例如:/mnt/sdcard/Pictures/Boat.jpg。但我只想要/mnt/sdcard/Pictures/

请不要告诉我使用 join 和 split,因为这不是我想要的。

【问题讨论】:

    标签: android path uri mediastore


    【解决方案1】:

    使用这个:

    String path = getExternalFilesDir(null).getAbsolutePath();
    

    在您的应用子文件夹中获取主目录,您可以在其中放置另一个目录,例如“Images”:

    String path = getExternalFilesDir(null).getAbsolutePath() + "/Images";
    File folder = new File(path);
    if (!folder.exists()) {
       folder.mkdir();
    }
    

    所以现在有了这个:

    String path = getExternalFilesDir(null).getAbsolutePath() + "/Images";
    

    您可以毫无问题地访问此文件夹。

    【讨论】:

      猜你喜欢
      • 2019-11-27
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 2019-05-01
      • 1970-01-01
      • 2012-10-10
      相关资源
      最近更新 更多