简体   繁体   English

如何从Media真实路径获取可写DocumentFile Uri?

[英]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. 用户授予的目录树( DocumentFile )。
  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. 我认为,这个https://stackoverflow.com/a/30514269/615025可以解决。 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. DocumentFile是一种新机制,可让我们修改Lollipop中的文件。 It is not the same case with this post how to get contact photo URI Thanks 如何获得联系人照片URI与本帖子不一样谢谢

You can open an outputStream directly from a Uri using ContentResolver.openOutputStream(Uri) For example, 您可以使用ContentResolver.openOutputStream(Uri)直接从Uri打开outputStream,例如,

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

Optionally, openOutputStream takes a string to specify mode. (可选) openOutputStream采用字符串来指定模式。 ie

openOutputStream(uri, "w");

to write or 写或

openOutputStream(uri, "wa");

to append. 追加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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