0

I have an android app that creates a file on external storage (e.g. an sd card). It can read and write the file perfectly as long as the Android OS is not restarted. After a restart, with the same android.net.Uri, used before to access the file, the app no longer can open the file to write in it.

The app has both READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions and gets a stream of the file using context.getContentResolver().openOutputStream(uri)) to write in it. but it fails after an android restart.

I have tested the app on two android phones and this happens in both.

How can I solve this problem?

Minimus Heximus
  • 2,683
  • 3
  • 25
  • 50
  • You may have better luck if you provide a [mcve] showing where this `Uri` comes from, where and how you are writing to it, and the specific symptoms (beyond "it fails"). My guess is that the `Uri` is coming from `ACTION_OPEN_DOCUMENT` (or `ACTION_CREATE_DOCUMENT`) and you failed to take persistable `Uri` permissions on it, but that is just a guess. – CommonsWare Jan 01 '21 at 18:47
  • @CommonsWare yes it is from ACTION_OPEN_DOCUMENT. I don't know how to take persistable Uri permissions. – Minimus Heximus Jan 01 '21 at 18:59
  • 1
    See https://stackoverflow.com/q/46916992/115145 for some examples, and see https://commonsware.com/blog/2020/08/08/uri-access-lifetime-still-shorter-than-you-might-think.html for more background on the situation. – CommonsWare Jan 01 '21 at 19:09

0 Answers0