一:系统提供的一个从数据库中获得缩略图的方法
我在这篇使用七牛云存储上传android本地视频并播放博客中使用了下面的android系统提供的api来获取视频的缩略图
bitmap = MediaStore.Video.Thumbnails.getThumbnail(mContentResolver, anInt, MediaStore.Video.Thumbnails.MICRO_KIND, null);
相对这个api,系统还在ThumbnailUtils中提供了一个
public static Bitmap createVideoThumbnail(String filePath, int kind)
但是第二个api因为是临时生成新的图片,因此在获取视频缩略图列表这样的页面中,耗费时间不可取。那么第一个api呢?getThumbnail()根据系统的注释,我们知道他可以检查指定的视频缩略图是否已经存在,如果不存就线程阻塞直到缩略图生成为止。
This method checks if the thumbnails of the specified image (origId) has been created.
It will be blocked until the thumbnails are generated.
二:getThumbnail方法分析
其实这个getThumbnail调用的是下面的这个方法
InternalThumbnails.getThumbnail(cr, origId,
InternalThumbnails.DEFAULT_GROUP_ID, kind, options,
EXTERNAL_CONTENT_URI, true)
而这个方法过长,我们只贴关心的部分
long magic = thumbFile.getMagic(origId);
if (magic != 0) {
if (kind == MICRO_KIND) {
synchronized (sThumbBufLock) {
if (sThumbBuf == null) {
sThumbBuf = new byte[MiniThumbFile.BYTES_PER_MINTHUMB];
}
if (thumbFile.getMiniThumbFromFile(origId, sThumbBuf) != null) {
bitmap = BitmapFactory.decodeByteArray(sThumbBuf, 0, sThumbBuf.length);
if (bitmap == null) {
Log.w(TAG, "couldn't decode byte array.");
}
}
}
return bitmap;
} else if (kind == MINI_KIND) {
String column = isVideo ? "video_id=" : "image_id=";
c = cr.query(baseUri, PROJECTION, column + origId, null, null);
if (c != null && c.moveToFirst()) {
bitmap = getMiniThumbFromFile(c, baseUri, cr, options);
if (bitmap != null) {
return bitmap;
}
}
}
magic是系统根据origId查询得到的缩略图对应的一个标志。我们看到当kind==MINI_KIND的时候,系统开始在缩略图表中查找缩略图。待会我们自己查表看看速度如何。
String column = isVideo ? "video_id=" : "image_id=";
c = cr.query(baseUri, PROJECTION, column + origId, null, null);
三:getThumbnail实际执行耗时
手机中有八个视频,那么看看每个视频执行getThumbnail()方法获取缩略图的耗时,单位为毫秒。
07-24 18:03:11.957 24473-25220/com.dzy E/HSL: ---->totalTime:1169
07-24 18:03:12.451 24473-25220/com.dzy E/HSL: ---->totalTime:494
07-24 18:03:12.981 24473-25220/com.dzy E/HSL: ---->totalTime:530
07-24 18:03:13.701 24473-25220/com.dzy E/HSL: ---->totalTime:720
07-24 18:03:14.334 24473-25220/com.dzy E/HSL: ---->totalTime:633
07-24 18:03:15.663 24473-25220/com.dzy E/HSL: ---->totalTime:1328
07-24 18:03:16.807 24473-25220/com.dzy E/HSL: ---->totalTime:1144
07-24 18:03:18.063 24473-25220/com.dzy E/HSL: ---->totalTime:1256
四:通过thumnail表单独查询速度快
通过MediaStore.Video.Thumbnails.getThumbnail()方法来查询缩略图耗时不可忍受。那么我们单独查表呢?
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Video.Media._ID));
thumbCursor = context.getContentResolver().query(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID
+ "=" + id, null, null);
if (thumbCursor.moveToFirst()) {
String path = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
long a = System.currentTimeMillis();
Bitmap bitmap= BitmapFactory.decodeFile(path);
bitmapArrayList.add(bitmap);
DebugUtil.error("---> string:"+ path);
DebugUtil.error("----> totalTime:"+(System.currentTimeMillis()-a));
}
我们先查表获得视频id,再通过视频id来查询thumbnail表,获得缩略图,执行耗时如下:
07-24 18:11:27.628 27272-27821/com.dzy E/HSL: ---> string:/storage/emulated/0/DCIM/.thumbnails/1497958474184.jpg
07-24 18:11:27.628 27272-27821/com.dzy E/HSL: ----> totalTime:11
07-24 18:11:27.640 27272-27821/com.dzy E/HSL: ---> string:/storage/emulated/0/DCIM/.thumbnails/1499264404259.jpg
07-24 18:11:27.641 27272-27821/com.dzy E/HSL: ----> totalTime:10
07-24 18:11:27.659 27272-27821/com.dzy E/HSL: ---> string:/storage/emulated/0/DCIM/.thumbnails/1499264401739.jpg
07-24 18:11:27.659 27272-27821/com.dzy E/HSL: ----> totalTime:14
07-24 18:11:27.673 27272-27821/com.dzy E/HSL: ---> string:/storage/emulated/0/DCIM/.thumbnails/1499331534347.jpg
07-24 18:11:27.673 27272-27821/com.dzy E/HSL: ----> totalTime:8
07-24 18:11:27.690 27272-27821/com.dzy E/HSL: ---> string:/storage/emulated/0/DCIM/.thumbnails/1499331534538.jpg
07-24 18:11:27.690 27272-27821/com.dzy E/HSL: ----> totalTime:12
07-24 18:11:27.701 27272-27821/com.dzy E/HSL: ---> string:/storage/emulated/0/DCIM/.thumbnails/1499331534915.jpg
07-24 18:11:27.701 27272-27821/com.dzy E/HSL: ----> totalTime:7
07-24 18:11:27.710 27272-27821/com.dzy E/HSL: ---> string:/storage/emulated/0/DCIM/.thumbnails/1499331535444.jpg
07-24 18:11:27.710 27272-27821/com.dzy E/HSL: ----> totalTime:5
07-24 18:11:27.719 27272-27821/com.dzy E/HSL: ---> string:/storage/emulated/0/DCIM/.thumbnails/1500352336527.jpg
07-24 18:11:27.719 27272-27821/com.dzy E/HSL: ----> totalTime:6
快了何止千百倍,难道是我对getThumbnail()方法理解错误?因为却少环境调试,无法进一步跟踪。