Android Question Android sdk problem?

Why my application run successfully on Android 8 but with Android >= 9 crash? in particular, it crashes when trying to create the com.appname folder and its related files including a database that is needed for the app to function

Crash at Line 9:
Sub CreoDB()
    If sql.IsInitialized Then sql.Close
    
    Comodo.DBDir = File.DirDefaultExternal
    
    Log("Creo DataBase su : " & Comodo.DBDir)
    
    
    sql.Initialize(Comodo.DBDir, "DATABASE.db", True)
    
    
    Dim schedatable      As String
    Dim utentetable      As String
    Dim eserciziotable   As String
    Dim applicativotable As String
    
    schedatable      = "CREATE TABLE IF NOT EXISTS Schede (id INTEGER PRIMARY KEY AUTOINCREMENT,nome TEXT,descrizione TEXT,data_inizio DATE,data_fine DATE,id_istruttore INTEGER,FOREIGN KEY (id_istruttore) REFERENCES istruttori(id));"
    utentetable      = "CREATE TABLE IF NOT EXISTS Utente (id BOOL PRIMARY KEY DEFAULT TRUE, nome TEXT NOT NULL, cognome TEXT NOT NULL, email TEXT NOT NULL UNIQUE, data_di_nascita TEXT NOT NULL, is_istruttore INTEGER DEFAULT 0)"
    eserciziotable   = "CREATE TABLE IF NOT EXISTS Esercizi (id INTEGER PRIMARY KEY AUTOINCREMENT,nome TEXT Not Null,descrizione TEXT,ripetizioni TEXT,serie INTEGER,recupero TEXT,peso TEXT, id_scheda INTEGER Not Null,FOREIGN KEY (id_scheda) REFERENCES Schede (id));"
    
    'Tabella per gestire il Versioning del DataBase e dell'Applicativo
    'L'id è un booleano con un vincolo di integrità sul valore 1(TRUE)
    'In tale modo non è possibile quindi avere più di una riga su questa tabella
    applicativotable = "CREATE TABLE IF NOT EXISTS Applicativo (id BOOL PRIMARY KEY DEFAULT TRUE, VersioneDB FLOAT DEFAULT 1.0, VersioneApp FLOAT DEFAULT 1.0)"
    
    
    'Creo le Tabelle nel DB
    sql.ExecNonQuery(schedatable)
    Log("Creata schede IF NOT EXISTS")
    
    sql.ExecNonQuery(utentetable)
    Log("Creata Utente IF NOT EXISTS")
    
    sql.ExecNonQuery(eserciziotable)
    Log("Creata Esercizi IF NOT EXISTS")
    
    sql.ExecNonQuery(applicativotable)
    Log("Creata Applicativo IF NOT EXISTS")
    
    Try
        'Aggiorno la tabella applicativo con i relativi suoi dati
        sql.ExecNonQuery("INSERT INTO Applicativo (VersioneDB, VersioneApp) VALUES (1.0, 1.0)")
        Log("Inserita versione 1.0 del DB e versione 1.0 dell'app")
    Catch
        Log("Tabelle già alterate")
    End Try
    
    

End Sub


and give me this error (see attach image)


PS: the app is over but i cannot recreate the problem because i work with a Huawei y7 2019 and i dont have a new Android
 

Attachments

  • a12e4445-1e87-4fc8-a2a2-c8c826ccca61.jpeg
    a12e4445-1e87-4fc8-a2a2-c8c826ccca61.jpeg
    468 KB · Views: 141
Tip: whenever you see code with File.DirDefaultExternal or File.DirRootExternal you can immediately tell that it is broken code.
so how can i fix this code? the problem i think born when i try to create the folder com.appname in Android/Data directory....so i dont know how can i fix this
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
the problem i think born when i try to create the folder com.appname in Android/Data directory . . .
Yes - you cannot access external storage without first getting the user's permission, and usually you have to let the user choose the folder. Here is a link that might be useful, but you will find many other posts on this forum covering this very common problem.

One option would be to replace "File.DirDefaultExternal" with "xui.DefaultFolder". This puts the database in the app's internal storage which might not be what you want but at least that would let you quickly check the rest of your code.

Another tip - if you right click on the Log pane in the B4A window you will be able to copy log messages to the clipboard.
 
Upvote 0
Yes - you cannot access external storage without first getting the user's permission, and usually you have to let the user choose the folder. Here is a link that might be useful, but you will find many other posts on this forum covering this very common problem.

One option would be to replace "File.DirDefaultExternal" with "xui.DefaultFolder". This puts the database in the app's internal storage which might not be what you want but at least that would let you quickly check the rest of your code.

Another tip - if you right click on the Log pane in the B4A window you will be able to copy log messages to the clipboard.
yes i know for copy log message but how i said i work with a Huawei y7 2019 and i dont have a new Android, and the message that i put like attach file stand for another device.

instead I solved it in this way: i change all File.DirDefaultExternal and File.DirRootExternal with File.DirInternal and finally work, work very well. the only thing is that in this way I don't see the folders in android/data/com.appname/files visible and therefore I have no way of being able to manage them for debugging tests
 
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
yes i know for copy log message but how i said i work with a Huawei y7 2019 and i dont have a new Android, and the message that i put like attach file stand for another device.

instead I solved it in this way: i change all File.DirDefaultExternal and File.DirRootExternal with File.DirInternal and finally work, work very well. the only thing is that in this way I don't see the folders in android/data/com.appname/files visible and therefore I have no way of being able to manage them for debugging tests
try to use https://www.b4x.com/android/forum/threads/runtime-permissions-android-6-0-permissions.67689/#content
B4X:
dim rp As RuntimePermissions
dim FileDir as String

FileDir=rp.GetSafeDirDefaultExternal("")
 
Upvote 0
Top