sanoj lawrence
sanoj lawrence

Reputation: 993

Unable to get all Video files from storage Android

Am using following method to get video files from storage. it retrieves only directories files but not read files inside sub-directory files.

Example:- i can read file from WhatsApp Video but in the same folder there is a sub folder name sent i can't read the files inside.

any help, how can i read all folder and sub-folder

public class GetVideos {

    @RequiresApi(api = Build.VERSION_CODES.Q)
    public void getAllVideosData(Context context, ArrayList<VideoViewInfo> AllVideosData) {

        String[] projection = new String[] {
                MediaStore.Video.Media._ID,
                MediaStore.Video.Media.TITLE,
                MediaStore.Video.VideoColumns.DATA,
                MediaStore.Video.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Video.Media.DATE_TAKEN,
                MediaStore.Video.Media.DURATION,
                MediaStore.Video.Media.RESOLUTION,
                MediaStore.Video.Media.SIZE
    };

        Uri Video = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String orderBy = MediaStore.Video.Media.DATE_TAKEN;

        @SuppressLint("Recycle")
        Cursor cur = context.getContentResolver().query(Video, projection,null,null,orderBy );
        assert cur != null;
        Log.i("ListingVideo"," query count=" + cur.getCount());

        if (cur.moveToNext()) {
            String id;
            String title;
            String filePath;
            String bucketName;
            String date;
            String duration;
            String resolution;
            String size;

            int tempId = cur.getColumnIndex(MediaStore.Video.Media._ID);
            int titleTemp = cur.getColumnIndex(MediaStore.Video.Media.TITLE);
            int filePathTemp = cur.getColumnIndex(MediaStore.Video.Media.DATA);
            int bucketTemp = cur.getColumnIndex(MediaStore.Video.Media.BUCKET_DISPLAY_NAME);
            int dateTemp = cur.getColumnIndex(MediaStore.Video.Media.DATE_TAKEN);
            int durationTemp = cur.getColumnIndex(MediaStore.Video.Media.DURATION);
            int resolutionTemp = cur.getColumnIndex(MediaStore.Video.Media.RESOLUTION);
            int sizeTemp = cur.getColumnIndex(MediaStore.Video.Media.SIZE);
            do {
                id = cur.getString(tempId);
                title = cur.getString(titleTemp);
                filePath = cur.getString(filePathTemp);
                bucketName = cur.getString(bucketTemp);
                date = cur.getString(dateTemp);
                duration = cur.getString(durationTemp);
                resolution = cur.getString(resolutionTemp);
                size = cur.getString(sizeTemp);
                Log.d(TAG, "getAllVideosData: "+bucketName);
                VideoViewInfo vvi = new VideoViewInfo(id, title, filePath, bucketName, date, duration, resolution, size);
                AllVideosData.add(vvi);

            } while (cur.moveToNext());
        }
    }
}

What should i do to get all video file in my storage ?

Bountry reward i want to read every video file from storage with above code i can only read files limited location as per MediaStore.Video

Upvotes: 1

Views: 1631

Answers (2)

Ali
Ali

Reputation: 529

It's really hard and full of a mess when doing file handling because you only access the files but what about the subdirectories in it? well in the case of

@Kursh's answer recursion will not help either

because it will just go deep into the sub directories never to come back I had developed the algorithm but it was not really inefficient, but you can use this library and remember to use it in an ASYNC TAK or Coroutine

Upvotes: 0

Kush Trivedi
Kush Trivedi

Reputation: 370

You can run a recursive function to list all video files like

void findVideos(File dir, ArrayList<String> list){
    for (File file : dir.listFiles()) {
        if (file.isDirectory()) findVideos(file, list);
        else if(file.getAbsolutePath().contains(".mp4")) list.add(file.getAbsolutePath());
    }
}

Or you can query the mediastore like done in this repository here

Upvotes: 3

Related Questions