Reputation: 329
I have implemented scoped storage & need to list the JPEG files from a third party app let's name it Test app which creates 'test' folder in following two path
In redmi K20pro running on Android 10 returns ArrayList on .listFiles irrespective of scoped storage.
However Nokia 6.1 running on Android 10 returns null if scoped storage is enabled & null on scoped storage enabled
Please help me the to fetch files from these 2 folders.
Upvotes: 2
Views: 1989
Reputation: 3307
Try the Mediastore way
for getting the image files from specific directory.
You need to pass the path of the directory with its parent directory.
private void loadImagesFromDir(Context context, String targetFolderPath) {
File targetFolder = new File(targetFolderPath);
String[] projection = new String[]{
MediaStore.MediaColumns._ID,
MediaStore.MediaColumns.DATA,
MediaStore.MediaColumns.BUCKET_DISPLAY_NAME,
MediaStore.MediaColumns.SIZE,
MediaStore.MediaColumns.DATE_ADDED
};
String selection = MediaStore.MediaColumns.BUCKET_DISPLAY_NAME + " LIKE ? ";
String[] selectionArgs = new String[]{"%" + targetFolder.getParentFile().getName() + "/" + targetFolder.getName() + "%"};
String selectionOrder = MediaStore.Images.Media.DATE_ADDED + " DESC";
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, selectionOrder
);
if (cursor != null) {
while (cursor.moveToNext()) {
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
String filePath =
cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
long dateAdded =
cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_ADDED));
}
cursor.close();
}
}
Upvotes: 0
Reputation: 73
it is maybe a bit late, but these 2 websites helped me:
You have to request the permission separately from the user to access it as with Android 9 and lower.
Hoped it helps.
Upvotes: 0
Reputation: 4283
I managed to use listFiles()
files on Android 10 as follows:
Add this permission to your project's AndroidManifest.xml file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
In this file, add the flag android:requestLegacyExternalStorage="true"
under your <application>
tag.
Request the READ_EXTERNAL_STORAGE permission from your user:
int PERMISSION_REQUEST = 1;
if (ActivityCompat.checkSelfPermission(this,
android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
String[] PERMISSIONS = {android.Manifest.permission.READ_EXTERNAL_STORAGE};
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_REQUEST);
}
Optionally, handle the result of the request:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, Do the you need to do.
Toast.makeText(getApplicationContext(), "permission was granted", Toast.LENGTH_SHORT).show();
} else {
// permission denied, Disable the functionality that depends on this permission.
Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
You can then use listFiles()
, for example to list the files in the phone's download dir:
File[] files = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).listFiles();
Log.i(getClass().getName(), "fileList = " + Arrays.toString(files));
Upvotes: 3