Amin Pinjari
Amin Pinjari

Reputation: 2173

Get list of files from specific Folder in Android Q

I am trying to access the specific folder in android Q but the app is getting crashed.

I am using below code which is working fine till Android PIE but it is not working in Android Q

    val directory = File(path)
    val files = directory.listFiles()


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Getting bellow error

NullPointerException: Attempt to get length of null array

Upvotes: 5

Views: 2141

Answers (1)

greywolf82
greywolf82

Reputation: 22183

With Android Q file interface can't be used to access public directories, unless you use the requestLegacyStorage in your app manifest (not raccomanded since it won't work anymore with Android R). To access the directory you need to use the DocumentFile class, for example:

DocumentFile f = DocumentFile.fromTreeUri(context, uri);
f.listFiles();

Upvotes: 2

Related Questions