我正在尝试从 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;
}