您可以使用 MediaStore api 从图库中加载视频。
// Represent the downloads collection
Uri downloadsCollection;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // if sdk is 29 or higher
downloadsCollection = MediaStore.Downloads.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
} else { // if sdk is lower than 29
downloadsCollection = MediaStore.Downloads.Media.EXTERNAL_CONTENT_URI;
}
/** Represents the list of which columns to return from query */
String[] projection = {
MediaStore.DownloadColumns._ID,
MediaStore.DownloadColumns.DATA,
MediaStore.DownloadColumns.WIDTH,
MediaStore.DownloadColumns.HEIGHT,
MediaStore.DownloadColumns.DISPLAY_NAME,
MediaStore.DownloadColumns.DATE_MODIFIED,
MediaStore.DownloadColumns.SIZE
};
/** Initializing the selection file types to select from the gallery */
String selection = MediaStore.DownloadColumns.MEDIA_TYPE + "=" + MediaStore.DownloadColumns.MEDIA_TYPE_VIDEO;
/** Initializing the order to sort the gallery by - date added descending */
val orderBy = MediaStore.DownloadColumns.DATE_ADDED + " DESC";
// Querying the gallery files collection, with the projection columns, the selection, and the order to sort by
Cursor cursor = getContentResolver().query(collection, projection, selection, null, orderBy);
/** Represents the id column */
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns._ID)
/** Represents the width column */
int widthColumn = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns.WIDTH)
/** Represents the height column */
int heightColumn = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns.HEIGHT)
/** Represents the display name column */
int displayNameColumn = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns.DISPLAY_NAME)
/** Represents the date added column */
int dateAddedColumn = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns.DATE_MODIFIED)
/** Represents the size column */
int sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.DownloadColumns.SIZE)
/** Represents the path to the file on disk column */
int filePathColumnIndex = cursor.getColumnIndex(MediaStore.DownloadColumns.DATA)
while (cursor.moveToNext()) { // While there are files in the gallery
/** Represents the id of the current file */
val id = cursor.getLong(idColumn)
/** Represents the value which is the uri related file local path */
val filePath = it.getString(filePathColumnIndex)
/** Represents the display name of the current file */
val displayName = it.getString(displayNameColumn)
/** Represents the width of the current file */
val width = it.getInt(widthColumn)
/** Represents the height of the current file */
val height = it.getInt(heightColumn)
/** Represent the date added of the current file in milliseconds */
val dateAdded = it.getLong(dateAddedColumn)
/** Represents the size of the current file */
var size = it.getLong(sizeColumn)
/** Represents the uri of the current file */
val contentUri = ContentUris.withAppendedId(downloadsCollection, id)
// todo: you can create a model object that will save all these values and then add it to a list
}
此代码用于加载下载集合中的所有视频,如果您想要特定的视频,您可以更改 selection 变量的值以仅匹配特定视频。
检索到所需的内容 uri 后,您可以使用 Glide 将视频缩略图加载到 ImageView:
Glide.with(context).load(contentUri).into(imageView)
或者只是使用 VideoView 并将内容 uri 设置为视频 uri 以播放视频。