Android Question File not found exception in apk promoted to playstore

Melchor99

Member
Licensed User
Longtime User
Hi,

My free apk has been published to googleplaystore a few minutes ago, compiled in API29 and jdk1.8 (api target 28)

My apk works fine in my mobile and in AVD emulator, but people is trying my app downloading it from googleplay store and receive next error message:

java.io.FileNotFoundException: storage/emulated/0/Android/data/com.myapp/files/myapp.db3

Apk contains the file myapp.db3 (i've verified it twice and it's in assets (Files path)) This .db3 is required for the app works. In code, I access to it in next way (Same way as did in my old apps in API14 jdk 1.6 which worked fine):

B4X:
If File.Exists(File.DirDefaultExternal , "myapp.db3") = False Then
        If FirstTime Then
            File.Copy(File.DirAssets, "myapp.db3", File.DirDefaultExternal, "ibeCycling.db3")
            SQL1.Initialize(File.DirDefaultExternal , "myapp.db3", True)
        End If
    Else
        If FirstTime Then
            SQL1.Initialize(File.DirDefaultExternal , "myapp.db3", True)
            sValidacion = SQL1.ExecQuerySingleResult ("SELECT count(*) FROM Cycling")
            If sValidacion = 0 Then
                SQL1.Close
                File.Delete(File.DirDefaultExternal, "myapp.db3")
                File.Copy(File.DirAssets, "myapp.db3", File.DirDefaultExternal, "myapp.db3")
                SQL1.Initialize(File.DirDefaultExternal , "myapp.db3", True)
            End If
        End If
    End If

I don't know what I'm doing wrong, this is my first app after some years and I don't know if I have to consider more points currently . Please, could someone guide me to a solution? code is working fine in my AVD in debug and release mode and in my mobile.

The issue is that people who installe the apk first in their mobiles is receiving the exception

EDIT: More info, it seems that issue is when trying to do the copy command:

B4X:
File.Copy(File.DirAssets, "myapp.db3", File.DirDefaultExternal, "ibeCycling.db3")

My feeling is that it's not possible to copy the file from File.DirAssets to File.DirDefaultExternal and I don't know if it can be due to permissions
 
Last edited:

sorex

Expert
Licensed User
Longtime User
it's better to use file.DirInternal , the one you try to use requires access from the user if I recall right.

so my guess is that it can't copy the file because the user didn't grant access.
 
Upvote 0

Melchor99

Member
Licensed User
Longtime User
Piece of code attached in my first post worked for me in my previous applications (API14 jdk 1.6) and in my avd virtual machine, so for this new app created after years I didn't check this part of the code

thanks for your replies, as soon as we can I will try to use file.DirInternal instead of File.DirDefaultExternal to avoid permissions requirements
and if this not works I will follow tutorial for RuntimePermissions, using the get safe dir external function applying required changes in manifest.

Ill update with results. Thanks a lot
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
make sure you check for the file in the old location. if it exists move it to the new location to make sure things work as expected without dataloss (if the user can update/add records)
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
Errr - in this piece of code:
B4X:
If File.Exists(File.DirDefaultExternal , "myapp.db3") = False Then
        If FirstTime Then
            File.Copy(File.DirAssets, "myapp.db3", File.DirDefaultExternal, "ibeCycling.db3")
            SQL1.Initialize(File.DirDefaultExternal , "myapp.db3", True)
        End If
...

The first time the app runs, it will copy myapp.db3 from DirAssets to DirDefaultExternal & rename it to ibeCycling.db3. Then it will try to initialize myapp.db3 in DirDefaultExternal - BUT, it doesn't exist (because you renamed it to ibeCycling.db3 when you copied it). So of course you'll get a file not found error...

- Colin.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…