如何使用ACTION_OPEN_DOCUMENT_TREE Intent获得真实路径? [英] How to get the real path with ACTION_OPEN_DOCUMENT_TREE Intent?
问题描述
我的应用下载并解压缩特定文件夹中的文件:
My app downloads and unzips a file in a specific folder:
output = new FileOutputStream(realpath, true);
output.write(buffer, 0, bytesRead);
ZipFile zipFile = new ZipFile(realpath);
使用新引入的ACTION_OPEN_DOCUMENT_TREE
Intent,我想为用户提供选择该文件夹的方法.
With the new introduced ACTION_OPEN_DOCUMENT_TREE
Intent, I would like to offer the user to choose that folder.
测试onActivityResult
中收到的值时,我得到的路径类似于/tree/primary:mynewfolder
,而不是像/sdcard/mynewfolder
这样的物理真实路径.
When testing the values received in my onActivityResult
, I get a Path like /tree/primary:mynewfolder
, which is not the physical real path like /sdcard/mynewfolder
.
Uri treeUri = data.getData();
String sPath = treeUri.getPath();
Log.v("Path from Tree ", sPath);
我的解压缩方法需要真实路径:
My unzip method need the real path:
ZipFile zipFile = new ZipFile(realpath);
如何从Lollipop(API 21& 22)中提供的URI中获取诸如/sdcard/mynewfolder
之类的真实路径?
How do I get the real path like /sdcard/mynewfolder
from the provided URI in Lollipop (API 21 & 22)?
推荐答案
从URI获取真实路径的过程与以前相同,但有一点点补充:
Process of getting real Path from URI is same as before, but with a little addition:
Uri uri = data.getData();
Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri,
DocumentsContract.getTreeDocumentId(uri));
String path = getPath(this, docUri);
具有中间方法的getPath()方法的要点可以在这里找到: getPath()
The gist of getPath() method with intermediate methods can be found here: getPath()
这篇关于如何使用ACTION_OPEN_DOCUMENT_TREE Intent获得真实路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!