简体   繁体   中英

How to get writable DocumentFile Uri from a Media real path?

I have a situation in Lollipop where I have:

  1. Directory tree ( DocumentFile ) which is granted from user.
  2. A collection of audio files that need to modify retrieved from media provider.

     Uri mediaUri = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, "45"); 

Then, how to write a stream into one of those files?

I think, this https://stackoverflow.com/a/30514269/615025 gives a work around. Copy bytes of the file to cache directory, do modification on the temporary file and then copy the bytes back to the original file.

DocumentFile is a new mechanism which allows us to modify files in Lollipop. It is not the same case with this post how to get contact photo URI Thanks

You can open an outputStream directly from a Uri using ContentResolver.openOutputStream(Uri) For example,

OutputStream fos;
fos = new BufferedOutputStream(context.getContentResolver().openOutputStream(uri));

Optionally, openOutputStream takes a string to specify mode. ie

openOutputStream(uri, "w");

to write or

openOutputStream(uri, "wa");

to append.

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