17

In Android Q introduced new Scoped storage feature, which says:

apps that target Android 10 (API level 29) and higher are given scoped access into external storage, or scoped storage, by default. Such apps have access only to the app-specific directory on external storage, as well as specific types of media that the app has created.

I have my app that creates SQLite database on external storage, such that when app uninstalls database still alive and can be used later as recovery or be used outside of Android device (let's say in PC)

How should I achieve the same effect with Android Q? More precisely if database stored in external public directory - how can I read this database using standard SQLiteOpenHelper?

Barmaley
  • 16,638
  • 18
  • 73
  • 146
  • You can't, at least not with the framework SQLite or the support SQLite AndroidX API (the one used by Room). I can't rule out some obscure solution using a custom-compiled SQLite library and perhaps a file descriptor. – CommonsWare Jan 10 '20 at 13:38
  • No, no. noo-oooo-oooo! I can't believe, there has to be a way to use SQLite database placed in public directory... – Barmaley Jan 10 '20 at 13:57
  • At start up and shut down of your app you could make a copy of the database file. Using the media store you need no permissions. – blackapps Jan 10 '20 at 15:38
  • @blackapps, sure I understand this option, though I'd be glad to have other approach as well: direct access to SQLite database on external storage – Barmaley Jan 13 '20 at 11:05
  • [AutoBackup](https://developer.android.com/guide/topics/data/autobackup.html) can be used for database file recovery. – art Jan 18 '20 at 13:50
  • Unfortunately size of autobackup is too small, my users used to store up to 1gb of personal data... – Barmaley Jan 18 '20 at 21:30

1 Answers1

5

You could try to use the solution from the docs:
https://developer.android.com/training/data-storage/compatibility

  • Target Android 9 (API level 28) or lower.
  • If you target Android 10 (API level 29) or higher, set the value of requestLegacyExternalStorage to true in your app's manifest file:
<manifest ... >
    <!-- This attribute is "false" by default on apps targeting Android 10 or higher. -->
    <application android:requestLegacyExternalStorage="true" ... >
    ...
    </application>
</manifest>
Merov
  • 1,028
  • 1
  • 14
  • 29
  • Looks like, it's best solution, though I'd be happy to find other solution. I just afraid that some time `android:requestLegacyExternalStorage` will disappear... – Barmaley Jan 18 '20 at 21:28
  • 1
    About disappearing as i understand it, we won't wait too long. `Scoped Storage will be required in next year’s major platform release for all apps, independent of target SDK level`. [Android Q Scoped Storage: Best Practices and Updates](https://android-developers.googleblog.com/2019/04/android-q-scoped-storage-best-practices.html) – Merov Jan 19 '20 at 21:38
  • 1
    @Merov yes it's finally here. the doomsday – M D P Jul 17 '21 at 16:18