Android获取本地视频及其缩略图

ContentProvider

在获取本地视频前我们先对Android四大组件之一的ContentProvider进行回顾。

问题1:ContentProvider的作用?

用户应用程序之间共享数据(比如我们要获取共享数据系统相册和系统视频),ContentProvider是程序间共享数据的一种协议的定义。
如果我们要将A应用的数据共享给B应用,就需要A应用重写ContentProvider来具体实现这种协议。

ContentProvider的作用简单的说就是定义一种通用的数据暴漏方式。

ContentProvider是一个抽象类,重写该类需要实现的方法如下:

  • onCreate() which is called to initialize the provider
  • query(Uri, String[], String, String[], String) which returns data to the caller
  • insert(Uri, ContentValues) which inserts new data into the content provider
  • update(Uri, ContentValues, String, String[]) which updates existing data in the content provider
  • delete(Uri, String, String[]) which deletes data from the content provider
  • getType(Uri) which returns the MIME type of data in the content provider

问题2:ContentResolver的作用?

我们试想这样一个场景,现在我们的应用A需要使用多个其他应用(BCD)所共享的数据,我们此时是不是需要知道BCD的ContentProvider的具体实现才会使用?所以Android为我们提供了ContentResolver来统一管理与不同ContentProvider间的操作。

我们可以通过在所有继承Context的类中通过调用getContentResolver()来获得ContentResolver。

那ContentResolver是如何来区别不同的ContentProvider的呢?这就涉及到URI(Uniform Resource Identifier)问题。传送门:ContentProvider中的URI

获取本地视频

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public List<EntityVideo> getList(Context context) {
List<EntityVideo> sysVideoList = new ArrayList<>();
// MediaStore.Video.Thumbnails.DATA:视频缩略图的文件路径
String[] thumbColumns = {MediaStore.Video.Thumbnails.DATA,
MediaStore.Video.Thumbnails.VIDEO_ID};
// 视频其他信息的查询条件
String[] mediaColumns = {MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA, MediaStore.Video.Media.DURATION};

Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media
.EXTERNAL_CONTENT_URI,
mediaColumns, null, null, null);

if (cursor == null) {
return sysVideoList;
}
if (cursor.moveToFirst()) {
do {
EntityVideo info = new EntityVideo();
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Video.Media._ID));
Cursor thumbCursor = context.getContentResolver().query(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID
+ "=" + id, null, null);
if (thumbCursor.moveToFirst()) {
info.setThumbPath(thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Video.Thumbnails.DATA)));
}
info.setPath(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media
.DATA)));
info.setDuration(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video
.Media.DURATION)));
sysVideoList.add(info);
} while (cursor.moveToNext());
}
return sysVideoList;
}

我们先来看看query方法这些参数的含义

1
public final Cursor query (Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

Parameters

  • uri The URI, using the content:// scheme, for the content to retrieve.
  • projection A list of which columns to return. Passing null will return all columns, which is inefficient.
  • selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.
  • selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.
  • sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered.

这种暴漏是不是很像我们查询数据库呢?我们可以给出我们的过滤和选择以及排序条件。

1
2
MediaStore.Video.Media.EXTERNAL_CONTENT_URI  //查询外部存储卡视频
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI //查询外部储纯卡视频缩略图