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:
Installing file.
PackageAdded: package:b4a.example
** Activity (main) Resume **
Timeline: Activity_idle id: android.os.BinderProxy@44d5f448 time:98649266
Installing file.
GC_CONCURRENT freed 370K, 14% free 50957K/58924K, paused 18ms+3ms, total 75ms
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:b4a.example
Failed to destroy process 22281
libcore.io.ErrnoException: kill failed: ESRCH (No such process)
at libcore.io.Posix.kill(Native Method)
at libcore.io.ForwardingOs.kill(ForwardingOs.java:86)
at java.lang.ProcessManager$ProcessImpl.destroy(ProcessManager.java:260)
at anywheresoftware.b4a.phone.Phone$LogCat.LogCatStop(Phone.java:773)
at anywheresoftware.b4a.b4abridge.service1._stoplogcat(service1.java:621)
at anywheresoftware.b4a.b4abridge.service1._streams_newdata(service1.java:682)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:170)
at anywheresoftware.b4a.BA$3.run(BA.java:315)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:212)
at android.app.ActivityThread.main(ActivityThread.java:5151)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
at dalvik.system.NativeStart.main(Native Method)
GC_CONCURRENT freed 399K, 14% free 50942K/58924K, paused 16ms+8ms, total 85ms
 

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.
 
  • Like
Reactions: eps
Upvote 0
Top