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

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

问题描述

在在奇巧的新画廊访问我得到了我的SD卡中的真实路径用这种方法

Before new gallery access in KitKat I got my real path in sdcard 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 in sdcard?

推荐答案

注意:这个答案的地址问题的一部分。对于一个完整的解决方案(在库的形式),看看用户保罗·伯克回答以下。

Note: This answer addresses part of the problem. For a complete solution (in the form of a library), look at user Paul Burke's answer below.

您可以使用URI来获得文件ID ,然后查询或者 MediaStore.Images.Media.EXTERNAL_CONTENT_URI MediaStore.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的奇巧新的存储访问架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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