2

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

Community
  • 1
  • 1
sancho21
  • 3,511
  • 1
  • 39
  • 45
  • possible duplicate of [Get content uri from file path in android](http://stackoverflow.com/questions/3004713/get-content-uri-from-file-path-in-android) – Vince May 19 '15 at 00:13
  • 1
    nope, not a duplicate. Completely different topic. – ssuukk Jun 12 '15 at 09:40

1 Answers1

2

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. i.e

openOutputStream(uri, "w");

to write or

openOutputStream(uri, "wa");

to append.

JohannB
  • 357
  • 6
  • 21