How to get the real path with ACTION_OPEN_DOCUMENT_TREE intentions. Lollipop API 21 and 22

My application downloads and unpacks a file in a specific folder:

output = new FileOutputStream(realpath, true); output.write(buffer, 0, bytesRead); ZipFile zipFile = new ZipFile(realpath); 

With the new intention ACTION_OPEN_DOCUMENT_TREE introduced, I would like to invite the user to select this folder.

When testing the values ​​obtained in my onActivityResult, I get Path as /tree/primary:mynewfolder , which is not a physical real path, like / sdcard / mynewfolder.

 Uri treeUri = data.getData(); String sPath=treeUri.getPath(); Log.v("Path from Tree ", sPath); 

My unpacking method needs a real way:

 ZipFile zipFile = new ZipFile(realpath); 

How to get the real path, for example / sdcard / mynewfolder, from the provided URI in Lollipop (API 21 and 22)?

+5
android android-5.0-lollipop
Apr 18 '15 at 6:05
source share
1 answer

The process of obtaining the real path from the URI is similar to the previous one, but with a small addition:

 Uri uri = data.getData(); Uri docUri = DocumentsContract.buildDocumentUriUsingTree(uri, DocumentsContract.getTreeDocumentId(uri)); String path = getPath(this, docUri); 

The getPath () method method with intermediate methods can be found here: getPath ()

+9
Apr 22 '15 at 6:32
source share



All Articles