'unable to list all video files in android

I want to list all the video files in sdcard and internal storage. The problem is that it is only accessing videos that are present in the root folder and not in the internal folders.Currently I only have one video in Path: /storage/emulated/0 and all the videos that are present inside these sub folders are not available.

 String path = Environment.getExternalStorageDirectory().toString();
    Log.e("TAG", "Path: " + path); => Path: /storage/emulated/0
    File directory = new File(path);
    File[] files = directory.listFiles();
    assert files != null;
    Log.e("TAG", "Size: "+ files.length);

    for (int i = 0; i < files.length; i++)
    {
        if (files[i].getName().contains(".mp4")){
            Log.e("TAG", "FileName:" + files[i].getName());
        }
    }

MediaScannerConnection.scanFile(this, new String[]{String.valueOf(getAllMedia())},
        new String[]{"video/*"},
        new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
                //Do something
                HashSet<String> videoItemHashSet = new HashSet<>();
                String[] projection = {MediaStore.Video.VideoColumns.DATA,
                        MediaStore.Video.Media.DISPLAY_NAME};
                Cursor cursor = getApplicationContext().getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        projection, null, null, null);
                try {
                    cursor.moveToFirst();
                    do {
                        videoItemHashSet.add((cursor.getString
                                (cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))));
                    } while (cursor.moveToNext());

                    cursor.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ArrayList<String> downloadedList = new ArrayList<>(videoItemHashSet);
               
              
            }
        });


Solution 1:[1]

  1. You should visit the folder recursively.

    Sample: How to recursively scan directories in Android

  2. A better way to get all the videos on the phone:

    public ArrayList<String> getAllMedia(Context context) {
        HashSet<String> videoItemHashSet = new HashSet<>();
        String[] projection = {MediaStore.Video.VideoColumns.DATA, MediaStore.Video.Media.DISPLAY_NAME};

        Uri collection;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
        } else {
            collection = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        }
        Cursor cursor = context.getContentResolver().query(collection, projection, null, null, null);
        try {

            if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                do {
                    videoItemHashSet.add((cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA))));
                } while (cursor.moveToNext());
            }

            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        ArrayList<String> downloadedList = new ArrayList<>(videoItemHashSet);
        return downloadedList;
    }

Don't forget to request the android.permission.READ_EXTERNAL_STORAGE permission.

If you cannot find the newest videos in the cursor, try refresh the MediaStore manually:

MediaScannerConnection.scanFile(
                this, new String[]{"/sdcard/DCIM/ or other folders"}, null, 
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {

                    }
                });

Doc: https://developer.android.com/training/data-storage/shared/media

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1