Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I list all file in a directory in android 10 using Android Studio?

My current code uses:

String DirectoryPath = "/storage/emulated/0";
File f = new File(DirectoryPath);
File[] file = f.listFiles();

The problem is that the array comes up as blank for anything outside:

/storage/emulated/0/Android/data/com.example.myProgram

From what I read online this no longer works with android 10+. So how would I list all files in a certain directory? (Making a file explorer as part of an App)

like image 756
Kevin K Avatar asked Oct 14 '25 04:10

Kevin K


2 Answers

Just add this line of code to your manifest in the application tag.

android: requestLegacyExternalStorage= "true"

like image 96
Johnny Avatar answered Oct 16 '25 17:10

Johnny


Yes, With old code it won't work. Here i found the solution

    List<Uri> filesList = new ArrayList<>();

    Uri collection;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
    } else {
        collection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    }

    String[] projection = new String[]{
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DISPLAY_NAME,
            MediaStore.Images.Media.SIZE
    };
    String selection = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
        selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME +
                " =? ";
    }
    String[] selectionArgs = new String[]{
            "Your folder name"
    };
    String sortOrder = MediaStore.Images.Media.DISPLAY_NAME + " ASC";
    Cursor cursor = getApplicationContext().getContentResolver().query(
            collection,
            projection,
            selection,
            selectionArgs,
            sortOrder
    );
    if (cursor != null && cursor.moveToFirst()) {
        Log.d("continue", "not null");
    } else {
        Log.d("continue", "null return");
        return;
    }
    // Cache column indices.
    int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
    int nameColumn =
            cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
    int sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);

    do {
        // Get values of columns for a given Images.
        long id = cursor.getLong(idColumn);
        String name = cursor.getString(nameColumn);
        int size = cursor.getInt(sizeColumn);

        Uri contentUri = ContentUris.withAppendedId(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
        Log.d("continue", "path-->" + contentUri);
        filesList.add(contentUri);

        // Stores column values and the contentUri in a local object
        // that represents the media file.
    } while (cursor.moveToNext());

In the above code, I written for images only if you want any other files audio video you need to replace Images with Audio and Video ...etc

Note: if you convert Uri to file or string, then you can't use those files. You will get an error in android 10

like image 39
mahesh kumar Avatar answered Oct 16 '25 19:10

mahesh kumar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!