Android Question Mysterious File.exists

Guenter Becker

Active Member
Licensed User
Hello,

I hace this code in my project
B4X:
If File.Exists(File.DirAssets,Theme) Then
    l = File.Readlist(File.DirAssets,Theme)
End If

I checked that the File called BlueTheme.TD is present in the Files folder and the folder is synchronized.
I replace the variable Theme with the full Filename "BlueTheme.TD".
I tested my project on my phone and on the emulator (Android 34).
I tested the code in different new projects.

I always got the Problem that File.exists is reporting false? Why does it not find the file?
Does ony one has an answer what goes wrong.
 

Guenter Becker

Active Member
Licensed User
Ok, my fault. Thank you agraham.
 
Upvote 0

walt61

Well-Known Member
Licensed User
Longtime User
It works in B4A, but not in B4J. Use lowercase characters only for your filename and it will work:
B4X:
    ' 2 files in Files: "BlueTheme.TD" and "bluetheme2.td":
    Log(File.Exists(File.DirAssets, "BlueTheme.TD")) ' False
    Log(File.Exists(File.DirAssets, "bluetheme2.td")) ' True

This way it will work for both products:
B4X:
#If B4A
Public Sub FileExistsInDirAssets(fname As String) As Boolean 'Tags: [files]

    Return File.Exists(File.DirAssets, fname)

End Sub
#Else If B4J
' Library dependencies: Reflection
Public Sub FileExistsInDirAssets(fname As String) As Boolean 'Tags: [files]

    ' Code based on https://www.b4x.com/android/forum/threads/file-exists-on-dirassets.138614

    Dim r As Reflector
    r.Target = Me
    r.Target = r.RunMethod("getClass")
    Dim In As InputStream = r.RunMethod2("getResourceAsStream", "/Files/" &  fname, "java.lang.String")
    If In.IsInitialized = False Then Return False
    In.Close
    Return True

End Sub
#End If
 
Upvote 0

Guenter Becker

Active Member
Licensed User
It works in B4A, but not in B4J. Use lowercase characters only for your filename and it will work:
B4X:
    ' 2 files in Files: "BlueTheme.TD" and "bluetheme2.td":
    Log(File.Exists(File.DirAssets, "BlueTheme.TD")) ' False
    Log(File.Exists(File.DirAssets, "bluetheme2.td")) ' True

This way it will work for both products:
B4X:
#If B4A
Public Sub FileExistsInDirAssets(fname As String) As Boolean 'Tags: [files]

    Return File.Exists(File.DirAssets, fname)

End Sub
#Else If B4J
' Library dependencies: Reflection
Public Sub FileExistsInDirAssets(fname As String) As Boolean 'Tags: [files]

    ' Code based on https://www.b4x.com/android/forum/threads/file-exists-on-dirassets.138614

    Dim r As Reflector
    r.Target = Me
    r.Target = r.RunMethod("getClass")
    Dim In As InputStream = r.RunMethod2("getResourceAsStream", "/Files/" &  fname, "java.lang.String")
    If In.IsInitialized = False Then Return False
    In.Close
    Return True

End Sub
#End If
Thank you this works. How should one know to use only lowercase? I hate Trial and Error.
 
Upvote 0
Top