如何列出设备上的所有视频文件

时间:2016-09-08 20:07:57

标签: android

我想在android(内部和外部存储器)中获取所有视频路径,我尝试过使用:

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());
    }
}

但我无法从我的设备中获取所有视频列表。

4 个答案:

答案 0 :(得分:19)

如果您想真正获得实际结果,那么您的解决方案必须尝试。

  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;
}

答案 1 :(得分:2)

您需要递归搜索。类似的东西:

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());
    }
}

答案 2 :(得分:0)

List videoItems = new ArrayList();

    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;

答案 3 :(得分:-2)

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))); 位图 vimage = 无效的; 尝试 { vimage = getApplicationContext().getContentResolver().loadThumbnail( contentUri, new Size(640, 480), null); } catch (IOException e) { e.printStackTrace(); }