0

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);
               
              
            }
        });
a awasi
  • 71
  • 6
  • `sdcard and internal storage. ...... the root folder ...internal folders.` You mentioned four locations. Unclear what you mean with each location. – blackapps Feb 12 '22 at 09:52
  • I mean that the path `/storage/emulated/0` contains one video and and the sub folders like `/storage/emulated/0/download` also contains videos but I can not access them. – a awasi Feb 12 '22 at 09:56
  • What do you mean with access? I think you only try to list them. Not read/write yet Please rewrite your post as you talk about four storage locations which is quite confusing. Put that info in your post too. – blackapps Feb 12 '22 at 09:57
  • are using android10 or above?.did you get other file? – lava Feb 12 '22 at 13:56
  • I am using android 10. – a awasi Feb 12 '22 at 14:16

1 Answers1

0
  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

simon5678
  • 249
  • 1
  • 5
  • Thank you for your answer but I tried similar code but the problem is that newly added videos are not added when you use this. You can see them in "File Manager" but not in your app. – a awasi Feb 12 '22 at 13:10
  • Try MediaScannerConnection.scanFile. – simon5678 Feb 12 '22 at 13:42
  • I have added in above code. But the result is same. – a awasi Feb 12 '22 at 13:45
  • Can you see if I am using it in the right way? – a awasi Feb 12 '22 at 13:53
  • 1. getAllMedia method return all the video files, not folders. 2. String.valueOf(getAllMedia()) will get the string of a list, like "[/sdcard/1.mp4, /sdcard/2.mp4]". 3. Try scan the root directory "/sdcard/" . – simon5678 Feb 12 '22 at 14:02