Android Question DirDefaultExternal and DirInternal with API 26

aaronk

Well-Known Member
Licensed User
Longtime User
Hi,

In my current app I am storing a SQLite database in File.DirDefaultExternal.

In my next app release, I plan to set the TargetSDK to API 26 which will require me to use runtime permissions. Currently in my released version it's using API 16.

Since users already have my app installed, and the file is stored in DirDefaultExternal, it will require the user to accept the permission or have they already accepted the permission since they have the app installed from when it was API 16 ?

I am planning to move the database file to File.DirInternal so I was going to do something like the following as soon as the app runs (maybe from the starter service?):

B4X:
Dim rp As RuntimePermissions
      If rp.Check(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) = True Then
        If File.Exists(File.DirDefaultExternal, "MyDatabase.db") Then
            File.Copy(File.DirDefaultExternal,"MyDatabase.db",File.DirInternal,"MyDatabase.db")
        End If
    End If

Is the above code the best way in moving the database from the old location (DirDefaultExternal) to the new location (DirInternal) providing that the user has access to that location ?
 

Mahares

Expert
Licensed User
Longtime User
Is the above code the best way in moving the database from the old location (DirDefaultExternal) to the new location (DirInternal) providing that the user has access to that location ?
Why do you need to move the database to Internal. You can still have it where it is and use this code in Starter service for RuntimePermissions:
B4X:
Public rp As RuntimePermissions
Public FilePath As String
Public SQL As SQL

'In Service_Create:
FilePath = rp.GetSafeDirDefaultExternal("")
SQL.Initialize(FilePath,"MyDatabase.db",True)
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Would the following code return a value if the Android device is an older device (for example Android device running API 18 and below) ?

B4X:
Dim rp As RuntimePermissions
       log(rp.GetSafeDirDefaultExternal(""))

I understand I need to add the following code to the manifest for older devices..

B4X:
AddManifestText(
<uses-permission
  android:name="android.permission.WRITE_EXTERNAL_STORAGE"
  android:maxSdkVersion="18" />
)

When I test it on my Android 8 device it works fine without having to request any permissions which is what I suspect, but I haven't got an old Android device to confirm if I am going to get the same result or not.

So does that mean the code above going to return a value with older devices, or do I still need to use File.DirDefaultExternal for older devices ?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
So does that mean the code above going to return a value with older devices
I have tested it with OS 4.1.2 API 16
log(rp.GetSafeDirDefaultExternal("")) returned the correct folder without reverting back to File.DirDefaultExternal and created a file MyDatabase.db
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
I have tested it with OS 4.1.2 API 16
log(rp.GetSafeDirDefaultExternal("")) returned the correct folder without reverting back to File.DirDefaultExternal and created a file MyDatabase.db
Great news. Thanks for checking!
 
Upvote 0
Top