Home > Mobile >  How to list subfolder content when using Android DocumentFile API
How to list subfolder content when using Android DocumentFile API

Time:11-03

I'm trying to switch to the DocumentFile API as Android doesn't allow using File API anymore...

When I select a folder, it returned URI is

content://com.android.externalstorage.documents/tree/primary:Media/Camera/FreeTime

Now I'm calling

 DocumentFile.fromTreeUri(this, uri)

to get the content of the folder. I want to keep a list of all the subfolders within

The 1st subfolder I found has the following URI

content://com.android.externalstorage.documents/tree/primary%3AMedia%2FCamera/FreeTime/document/primary%3AMedia%2FCamera%2FFreeTime/11

I need to save it so later on I can check the files within that folder

The problem is that when I try to call

 DocumentFile.fromTreeUri(this, uri)

on this subfolder uri it returns a DocumentFile matching the parent folder of this subfolder

content://com.android.externalstorage.documents/tree/primary:Media/Camera/FreeTime

How can I save the subfolder uri here so I can later on list this subfolder files?

CodePudding user response:

How can I save the subfolder uri here so I can later on list this subfolder files?

Two things.

First: take persistable uri permission on the folder choosen by the user.

Second: a subfolder uri as you save it is no tree uri and hence you cannot create a DocumentFile with .fromTreeUri().

Instead use:

 .fromSingleUri().

CodePudding user response:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.google.android.packageinstaller/com.android.packageinstaller.permission.ui.GrantPermissionsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2895) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1616) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:176) at android.app.ActivityThread.main(ActivityThread.java:6651) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.computePermissionGrantState(GrantPermissionsActivity.java:394) at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.updateDefaultResults(GrantPermissionsActivity.java:452) at com.android.packageinstaller.permission.ui.GrantPermissionsActivity.onCreate(GrantPermissionsActivity.java:136) at android.app.Activity.performCreate(Activity.java:7088) at android.app.Activity.performCreate(Activity.java:7079) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) ... 9 more

  • Related