简体   繁体   中英

How to save file to Uri path in Android

I have a Uri path that the user has granted through ACTION_OPEN_DOCUMENT_TREE.

I save this Uri as string representation to SharedPreferences. Now I want to copy an internal file to a sub folder of that folder and give it the same file name as the internal file.

I use this to get a DocumentFile to the uriTree:

Uri folder = Uri.parse(<getmysaveduristring>);
DocumentFile file = DocumentFile.fromTreeUri(c, folder);

How do I edit the DocumentFile to include a subfolder and a file name?

Uris and DocumentFiles are confusing me a lot at the moment.

Got it like this:

DocumentFile subDirFile = file.createDirectory(subDir);
DocumentFile actualFile = subDirFile.createFile("*/*", dst.getName());

Then use

OutputStream out = c.getContentResolver().openOutputStream(actualFile.getUri());

to open the file.

Took me a while to figure out that using DocumentFiles was essential.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM