简体   繁体   中英

Random access write to SAF file on Android

I am trying to implement "random access" write to a stream retrieved from Android Storage Access Framework.

In my scenario, I pick a file using OPEN_DOCUMENT intent which gives me a document URI. Then I can use the ContentResolver to openOutputStream . From that point though, I don't see a way to "seek" within the stream to write on a given position within the file.

Is there a way to achieve this (at least for local storage/SD card-based files)?

at least for local storage/SD card-based files

You do not know that is what you are getting. The user can choose any document provider the user wants (eg, Google Drive).

I don't see a way to "seek" within the stream to write on a given position within the file

Partly, that's because Java streams suck. But, mostly, it's for flexibility for the document provider. Suppose the user chooses a provider that stores its content locally, but encrypted by a stream cipher. You can't then just jump to some arbitrary spot — the data has to be decrypted along the way.

AFAIK, your only option is to copy the content to some file that you control, after which you have random access to that file.

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