Search code examples
androidlocal-storagestoragestorage-access-framework

Accessing Images in Android External Storage after reinstall application


We are implementing a backup/restore system for the app. We used Google Drive API as documented in the Android Guide

We also store image URIs in the database. In the restoring process, we get URIs but lost permission to reach images when we re-install the app. We have the following exception.

Failed query: java.lang.SecurityException: Permission Denial: opening provider ... requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs

We get image URI via "ACTION_OPEN_DOCUMENT" but can't persist it after deleting/install the application. What are the related APIs? Do we need to move images in an app-specific folder and backup images itself also?

Thank you for reading this, any help is appreciated


Solution

  • What are the related APIs?

    ACTION_OPEN_DOCUMENT_TREE and ACTION_CREATE_DOCUMENT are probably what they are referring to.

    Do we need to move images in an app-specific folder and backup images itself also?

    We cannot really answer that — that is a business decision as much as a technical one. However, if your app is uninstalled and reinstalled:

    • You lose access to content identified by Uri values that you obtained from the Storage Access Framework
    • On Android 10+, you lose access to any files that your older app installation created that survived the uninstall process (e.g., they were in a shared collection)

    How you work around that is up to you. In addition to your proposal, you could:

    • Have all the images be stored in a single directory tree (obtained via ACTION_OPEN_DOCUMENT_TREE), store relative paths in your database, and request access to that tree again after your app is restored
    • Remove all of the Uri values from the database before backing it up and live without access to those images if the database is restored
    • Store the image data in the database itself (typically not recommended but technically possible)