I have the following code to handle storage permissions :
private void getGalleryPermission() {
Log.d(TAG, "getGalleryPermission: checking permissions.");
String[] permissions = { WRITE_STORAGE, READ_STORAGE };
if (ContextCompat.checkSelfPermission(requireActivity(), WRITE_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "getGalleryPermission: write storage is granted.");
if (ContextCompat.checkSelfPermission(requireActivity(), READ_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "getGalleryPermission: read storage is granted.");
setupRecyclerView();
}
else {
Log.d(TAG, "getGalleryPermission: asking to read the storage.");
requestPermissions(permissions, REQUEST_STORAGE_PERMISSION_RESULT);
}
}
else {
Log.d(TAG, "getGalleryPermission: asking to write the storage.");
requestPermissions(permissions, REQUEST_STORAGE_PERMISSION_RESULT);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.d(TAG, "onRequestPermissionsResult: request code " + requestCode);
if (requestCode == REQUEST_STORAGE_PERMISSION_RESULT) {
Log.d(TAG, "onRequestPermissionsResult: request code is correct.");
if (grantResults.length > 0) {
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(requireActivity(), "Don't have permission to access the Gallery !", Toast.LENGTH_SHORT).show();
break;
}
}
// Setup the gallery once the permissions are granted
setupRecyclerView();
}
}
}
I am testing this code on several of my phones, and it's working perfectly. However, on my Huawei, the listFiles()
method returns null
on the /storage/emulated/0 directory
.
I have checked in the phone Settings + in logs, and the storage permissions are granted (like on other working phones).
What can prevent the files from being read despite granted permissions ?
On some phones, is it possible that the /storage/emulated/0 is not the correct directory to target to get all gallery files ?
PS : i have checked the directory from the Files application in my phone and it's not empty (DCIM, etc.... are inside).
Thanks
is the Huawei device which returns null
running Android 10? It seems that for that path Android returns null
since API 29. You might want to check this question here.