Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list all video files on device

Tags:

android

I want to get all videos path in android (Internal and External storage both), I have tried use:

List<String> paths = new ArrayList<String>();
File directory = new File("/system" OR "/mnt/sdcard");
File[] files = directory.listFiles();
for (int i = 0; i < files.length; ++i) {
    if(files[i].getAbsolutePath().contains(".mp4")) {
       paths.add(files[i].getAbsolutePath());
    }
}

but I can not get all of video lists from my device.

like image 259
soheila Avatar asked Sep 08 '16 20:09

soheila


3 Answers

Here is your solution must try if you want to really a actual result.

  public ArrayList<String> getAllMedia() {
    HashSet<String> videoItemHashSet = new HashSet<>();
    String[] projection = { MediaStore.Video.VideoColumns.DATA ,MediaStore.Video.Media.DISPLAY_NAME};
    Cursor cursor = getContext().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);
    return downloadedList;
}
like image 111
Bilal Mustafa Avatar answered Oct 08 '22 23:10

Bilal Mustafa


You need to make your search recursive. Something 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());
    }
}
like image 32
Mick Avatar answered Oct 08 '22 22:10

Mick


    List<VideoClass> videoItems = new ArrayList<VideoClass>();

    ContentResolver contentResolver = getContentResolver();
    Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

    Cursor cursor = contentResolver.query(uri, null, null, null, null);

    //looping through all rows and adding to list
    if (cursor != null && cursor.moveToFirst()) {
        do {

            String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE));
            Uri contentUri = ContentUris.withAppendedId(
                    MediaStore.Video.Media.EXTERNAL_CONTENT_URI, cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)));
            Bitmap vimage =
                    null;
            try {
                vimage = getApplicationContext().getContentResolver().loadThumbnail(
                        contentUri, new Size(640, 480), null);
            } catch (IOException e) {
                e.printStackTrace();
            }
            VideoClass  videoModel  = new VideoClass();
            videoModel.setTitle(title);
            videoModel.setMain_video(String.valueOf(contentUri));
            videoModel.setImage(vimage);
            videoItems.add(videoModel);

        } while (cursor.moveToNext());
    }
    return videoItems;
like image 43
user16643466 Avatar answered Oct 08 '22 21:10

user16643466