Android Question Problem with File.ListFiles on an external sdcard with Android 4.4.2

klaus

Expert
Licensed User
Longtime User
I recently upgraded my sony xperia to Android 4.4.2.
Now I have a problem with an application reading files on the external sd card.
The code below works on previous versions of Android.
B4X:
Sub Activity_Create(FirstTime As Boolean)
    lblFiles.Initialize("")
    Activity.AddView(lblFiles, 10%x, 10%y, 80%x, 80%y)
    Dim lst0 As List
    lst0 = File.ListFiles("/sdcard1/")
   
    For i = 0 To lst0.Size - 1
        lblFiles.Text = lblFiles.Text & CRLF & lst0.Get(i)
    Next
End Sub
But now it raises an error 'object must first be initialized' which means that the List lst0 is Null.
It raises the error only in release mode but it works in Debug(legacy) mode B4ABridge Bluetoth !?

The unfiltered log shows this:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I guess that you are missing the read_external permission.

It will be added automatically if there is any reference to File.DirRootExternal or File.DirDefaultExternal.
You can also add it with the manifest editor:
B4X:
AddPermission(android.permission.READ_EXTERNAL_STORAGE)

The debugger adds it automatically.
 
Reactions: eps
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…