Despite all the required permissions, I somehow cannot list the files in a folder.
This is my code: (Android 10)
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
[...]
android:requestLegacyExternalStorage="true"
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// CHECK PERMISSIONS
if (Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((MainActivity) this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else{
// LIST FILES
File directory = Environment.getExternalStorageDirectory();
directory.exists(); // TRUE
directory.isDirectory(); // TRUE
File[] files = directory.listFiles(); // NULL [!]
}
}
Nevertheless, directory.listFiles()
still returns null. It does reach directory.listFiles()
, so the Permissions are granted. The directory exists and is a directory (both returns true).
I've actually gone through all of the threads on the subject, but couldn't find a solution. I hope I haven't missed anything...
Thanks for all answers :]
Despite all the required permissions, I somehow cannot list the files in a folder.
This is my code: (Android 10)
AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
[...]
android:requestLegacyExternalStorage="true"
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// CHECK PERMISSIONS
if (Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((MainActivity) this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else{
// LIST FILES
File directory = Environment.getExternalStorageDirectory();
directory.exists(); // TRUE
directory.isDirectory(); // TRUE
File[] files = directory.listFiles(); // NULL [!]
}
}
Nevertheless, directory.listFiles()
still returns null. It does reach directory.listFiles()
, so the Permissions are granted. The directory exists and is a directory (both returns true).
I've actually gone through all of the threads on the subject, but couldn't find a solution. I hope I haven't missed anything...
Thanks for all answers :]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.