从URI中获取真实路径,Android KitKat新的存储访问框架 [英] Get real path from URI, Android KitKat new storage access framework

查看:34
本文介绍了从URI中获取真实路径,Android KitKat新的存储访问框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 4.4 (KitKat) 中的新画廊访问之前,我得到了我的真实使用此方法在 SD 卡上的路径:

Before the new gallery access in Android 4.4 (KitKat) I got my real path on the SD card with this method:

public String getPath(Uri uri) {
   String[] projection = { MediaStore.Images.Media.DATA };
   Cursor cursor = managedQuery(uri, projection, null, null, null);
   startManagingCursor(cursor);
   int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
   cursor.moveToFirst();
 return cursor.getString(column_index);
}

现在,Intent.ACTION_GET_CONTENT 返回不同的数据:

Now, the Intent.ACTION_GET_CONTENT return different data:

之前:

content://media/external/images/media/62

现在:

content://com.android.providers.media.documents/document/image:62

如何获取SD卡上的真实路径?

How could I manage to obtain the real path on the SD card?

推荐答案

注意:这个答案解决了部分问题.有关完整的解决方案(以库的形式),请查看 保罗·伯克的回答.

您可以使用 URI 获取 document id,然后查询 MediaStore.Images.Media.EXTERNAL_CONTENT_URIMediaStore.Images.Media.INTERNAL_CONTENT_URI(视SD卡情况而定).

You could use the URI to obtain document id, and then query either MediaStore.Images.Media.EXTERNAL_CONTENT_URI or MediaStore.Images.Media.INTERNAL_CONTENT_URI (depending on the SD card situation).

获取文档ID:

// Will return "image:x*"
String wholeID = DocumentsContract.getDocumentId(uriThatYouCurrentlyHave);

// Split at colon, use second item in the array
String id = wholeID.split(":")[1];

String[] column = { MediaStore.Images.Media.DATA };     

// where id is equal to             
String sel = MediaStore.Images.Media._ID + "=?";

Cursor cursor = getContentResolver().
                          query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                          column, sel, new String[]{ id }, null);

String filePath = "";

int columnIndex = cursor.getColumnIndex(column[0]);

if (cursor.moveToFirst()) {
    filePath = cursor.getString(columnIndex);
}   

cursor.close();

参考:我找不到该解决方案来自的帖子.我想请原始海报在这里做出贡献.今晚再看看.

Reference: I'm not able to find the post that this solution is taken from. I wanted to ask the original poster to contribute here. Will look some more tonight.

这篇关于从URI中获取真实路径,Android KitKat新的存储访问框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆