1

我正在尝试从 Android 11 中的 MediaStore.Video 检索视频内容。光标返回“0”,代码如下。我在清单文件中添加了 READ_EXTERNAL_STORAGE 权限,并在访问以下代码之前在活动中添加了运行时权限检查。您能帮我如何从 Android 11 的 MediaStore.Video 提供程序中的所有视频中获取名称、持续时间、大小字段吗?

    private List<MediaData> getMediaContent() {
    List<MediaData> mediaList = new ArrayList<MediaData>();
    String[] PROJECTION = new String[]{
            MediaStore.Video.Media._ID,
            MediaStore.Video.Media.DISPLAY_NAME,
            MediaStore.Video.Media.DURATION,
            MediaStore.Video.Media.SIZE
    };

    String selection = MediaStore.Video.Media.DURATION + ">= ?";

    String[] selectionArgs = new String[]{
            String.valueOf(TimeUnit.MILLISECONDS.convert(5, TimeUnit.HOURS))};
    String sortOrder = MediaStore.Video.Media.DISPLAY_NAME + " ASC";

    try {
        Cursor cursor = getContentResolver().query(MediaStore.Video.Media.INTERNAL_CONTENT_URI, PROJECTION, selection, selectionArgs, sortOrder);
        while (cursor.moveToNext()) {
            long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
            String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));
            int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
            int size = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));

            Uri uri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, id);
            mediaList.add(new MediaData(uri, name, duration, size));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return mediaList;
}
4

1 回答 1

-1

愿这对您有所帮助:

private final Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;

public Cursor getandroidPlaylistcursor(Context context) {
    ContentResolver resolver = context.getContentResolver();
    final String id = MediaStore.Audio.Playlists._ID;
    final String name = MediaStore.Audio.Playlists.NAME;
    final String[] columns = { id, name };
    return  resolver.query(uri, columns, null, null,name + " ASC");
}

但如果你使用 Mefiaplayer :

MediaPlayer mp = MediaPlayer.create(this, Uri.parse(uriOfYourFile));
 int duration = mp.getDuration();
etc(...)
于 2020-11-07T13:31:51.093 回答