Android Question Use imposed folder for read (and write) database

bernardR

Member
Licensed User
Longtime User
Hello,

Here is my problem:
I am using an old application to write to a database (targetSDK = 22).
This "perso.bd" database is in a folder at the root of my internal storage ("/ storage / emulated / 0 / mydatabase")
It works great, even on android 10.

I have programmed four years ago an app that uses the same database for reading.
With targetSDK = 22, no problem.

I modified targetSDK by trying 23, 29 or 30 on an Android 10 smartphone.
In all cases: Error during database initialization.

B4X:
Dim maBD As SQL
maBD.Initialize("/storage/emulated/0/mydatabase","perso.bd",True)
'>>>> Erreur "android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14 SQLITE_CANTOPEN): Could not open database"

I requested and got different permissions (rp.PERMISSION_READ_EXTERNAL_STORAGE and rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
Nothing changes, the error persists.

I cannot change the folder in which "perso.bd" is placed by the other application.
Is there a way to get my app (non-google) to work with targetSDK = 29 or 30?
I have tried some ideas from this forum without success.

Thank you for your ideas and your examples !
 

Mahares

Expert
Licensed User
Longtime User
Is there a way to get my app (non-google) to work with targetSDK = 29
Use Target SDk =29 and implement runtimepermissions:
B4X:
Public rp As RuntimePermissions  'need runtimepermissions lib, in starter service
Public  DBFilePath = File.DirRootExternal & "/mydatabase"    'in starter service
Public maDB as SQl   ' starter service

Sub Activity_Create(FirstTime As Boolean)
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)  '
    Wait For Activity_PermissionResult (Permission As String, PResult As Boolean)  'use B4XPage instead of Activity if B4XPages project
    If PResult = False Then
        MsgboxAsync("No permission to access external storage", "")
        Return
    Else
        Starter.maBD.Initialize(Starter.DBFilePath,"perso.bd",True)
    End If
End Sub
For target 30, read more about runtime permissions, what you can access in terms of folders and what you can't. So much written about it in forrum.
 
Upvote 1

bernardR

Member
Licensed User
Longtime User
Thank you Mahares.
Now It works.
Why rp in starter service ?

I have not an device android 11 for the moment.
I have read the changes, especially about 'scoped storage'. But I don't see how to adapt it to my problem.

Does somebody have an idea ?
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
Is it possible?
Yes, but only for your own use. Google will not allow it in store apps.

 
Upvote 0
Top