Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android如何获取媒体商店图像的唯一uri?_Android_Path_Uri_Mediastore - Fatal编程技术网

Android如何获取媒体商店图像的唯一uri?

Android如何获取媒体商店图像的唯一uri?,android,path,uri,mediastore,Android,Path,Uri,Mediastore,我正在尝试获取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[]

我正在尝试获取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/

请不要告诉我使用“加入”和“拆分”,因为这不是我想要的。

使用以下方法:

String path = getExternalFilesDir(null).getAbsolutePath();
要获取应用程序子文件夹中的主目录,您可以通过以下方式将另一个目录(例如“图像”)放入其中:

String path = getExternalFilesDir(null).getAbsolutePath() + "/Images";
File folder = new File(path);
if (!folder.exists()) {
   folder.mkdir();
}
因此,现在有了这个:

String path = getExternalFilesDir(null).getAbsolutePath() + "/Images";
您可以毫无问题地访问此文件夹