Android Question [SOLVED] File not found

TyneBridges

Member
Licensed User
Longtime User
I've seen this error in other posts but I think their details are not quite the same. I hope this one is fairly simple, but it's stumped me.

My application worked fine until I installed B4A on a newer PC along with all the additional requirements and recompiled on the new machine. The application compiles file but I now get a "File not found" runtime error when I try to load my text. This happens on the emulator as well as my Android tablet. I can see the text file in question in my Files folder on the PC (File.DirAssets) and have renamed it as simply as possible (data.txt) to avoid uppercase/lowercase issues but I still get the error.

As far as I can see, the only thing that's different between the two PCs is that Java etc are installed on the new PC on Drive I, not C. However, if the file paths were wrong, shouldn't the app fail to compile? I can't see any options for setting application paths other than the obvious ones in Tools | Configure Paths.

Here is some of my code.
B4X:
Sub Activity_Create(FirstTime As Boolean)
Dim RCount As Int, N As Int, SU As StringUtils, OurList As List, X As Int, S() As String
    ' Load file created with Visual Designer
    Activity.LoadLayout("DiaryMain")
    BFBtn.Text = "Browse recent"
    StatusLbl.Text = "Ready"
    SrchBtn.Enabled = True
    Try
        'DBFilename is defined in module G (Global variables)
        G.SQL1.Initialize(G.DBFileDir, G.DBFileName, True)      
        RCount = G.SQL1.ExecQuerySingleResult("Select count(*) from DEntries")  
        StatusLbl.Text = RCount & " entries in diary"
    Catch
        StatusLbl.Text = "No database found. Use IMPORT to create it."
    End Try
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub CopyFileFromAssets (FileName As String) As String ' Note: this will overwrite without prompting
Dim TargetDir As String = File.DirRootExternal
    If File.Exists(TargetDir, FileName) Then
         File.Delete(TargetDir, FileName)
       End If
    Try
        ' File.DirAssets is the Files folder within our app. Is read-only - that is, we can't delete
        File.Copy(File.DirAssets, FileName, TargetDir, FileName)
           Return TargetDir
        ' Delete the source file to save space
        'File.Delete(File.DirAssets, FileName)
        'MsgboxAsync("Deleted file " & FileName & " from " & File.DirAssets, G.AppName)
    Catch
        MsgboxAsync("Error! File " & FileName & " not found in folder " & File.DirAssets, G.AppName)
    End Try
End Sub

Sub SrchBtn_Click
    StartActivity("Search")  
End Sub

Sub ImpBtn_Click
Dim N As Int, OurLoc As String, SU As StringUtils, OurList As List, X As Int, Recd() As String
    G.TFName = "data.txt" ' This name is CASE SENSITIVE so keep it simple!
    'NOTE brackets after S, denoting this is a string ARRAY
    ' Create database if this does not already exist
    StatusLbl.Text = "Importing data. Please wait..."
    StatusLbl.Invalidate ' Force redraw
    OurLoc = CopyFileFromAssets(G.TFName) 'Get data file from assets
 

Mahares

Expert
Licensed User
Longtime User
I hope this one is fairly simple, but it's stumped me.
The most obvious thing in your code is you use File.DirRootExternal. For that you must request and use runtimepermissions. Here is the link to help you implement it:
 
Upvote 0

TyneBridges

Member
Licensed User
Longtime User
Thanks Mahares. I can see that's an issue that has cropped up in recent versions of Android (security has become a dirty word with me). I'll have to put some effort into following what the link is telling me.

What I definitely don't understand is why it's still working on the older PC and on my Android phone and not the newer one and my tablet. The only difference is in my application source drive: B4A and Java versions and even Windows 10 are all the same between the two PCs. As far as I can see, this particular problem isn't with device permissions (or it wouldn't run on my Android 10 phone) but the output from B4A on this PC.
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I'll have to put some effort into following what the link is telling me.
Here is a complete simple project attached to give you a start, although requesting permission could also be in a button click or somewhere else:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Dim rp As RuntimePermissions   'need runtimepermissions lib cheched
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    wait for Activity_PermissionResult(Permision As String, Result As Boolean)
    If Result = False Then
        MsgboxAsync("No permission to access external storage", "")
        Return
    Else
        Log("granted")
    End If
    File.Copy(File.DirAssets,"countriespopulations2019.csv", File.DirRootExternal, "countriespopulations2019.csv")
End Sub
When you run it the screen will prompt you for permission, click yes. But, it is highly advised to read and see the video from Erel about rtp.
 

Attachments

  • FileCopyAssetstoDIrRootExternalRtp.zip
    11.7 KB · Views: 148
Upvote 0

TyneBridges

Member
Licensed User
Longtime User
Thanks for your help, Mahares. After adding your code, changing all references to DirRootExternal to DirInternal and recompiling, I now get prompted on the emulator and the tablet to grant permission to access storage, which I do. However, I still then get the frustratingly unspecific error 14 SQLITE_CANTOPEN, so I do think the problem must be something connected to my B4A installation. I ran your project and that also gave a "File not found" error at runtime on my system (see the attached image)...
 

Attachments

  • Emulator.JPG
    Emulator.JPG
    43.6 KB · Views: 148
Last edited:
Upvote 0

TyneBridges

Member
Licensed User
Longtime User
Could it be that, because B4A is installed on the C: drive and my app and Java libraries are on I:, B4A is somehow creating incorrect virtual directory mappings - that is, the files (in Mahares' example as well as my own app) are not found because the app is looking in the wrong directory? I don't know how to check that the physical directory is the one where the app is looking.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Could it be that, because B4A is installed on the C: drive and my app and Java libraries are on I:
Don't use the emulator to test your app. I have seen so many times Erel rightfully advising against using it. Use a real device like a tablet or phone. If you are still having problems, you need to export your project as zip to the forum. There are a lot of smart dudes that are willing to help you.
 
Upvote 0

TyneBridges

Member
Licensed User
Longtime User
Thanks, Mahares. It looks as if permissions were the answer. I found a stupid error in my code that meant I was trying to open the database file in a directory/folder with no name!
 
Upvote 0
Top