设为首页 加入收藏

TOP

Android 获取网络视频缩略图【完美兼容各版本】
2014-11-23 22:00:03 来源: 作者: 【 】 浏览:22
Tags:Android 获取 网络视频 缩略 完美 兼容 版本

方案一:感谢@xjxuvup 提供


鉴于多数人都用过ThumbnailUtils.createVideoThumbnail()方法,该方法在2.x系统下可用,API LEVEL > 14时却只能返回null,以下为解决该问题方案:


重写createVideoThumbnail()方法,如下


@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)


private Bitmap createVideoThumbnail(String url, int width, int height) {
Bitmap bitmap = null;
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
int kind = MediaStore.Video.Thumbnails.MINI_KIND;
try {
if (Build.VERSION.SDK_INT >= 14) {
retriever.setDataSource(url, new HashMap());
} else {
retriever.setDataSource(url);
}
bitmap = retriever.getFrameAtTime();
} catch (IllegalArgumentException ex) {
// Assume this is a corrupt video file
} catch (RuntimeException ex) {
// Assume this is a corrupt video file.
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
// Ignore failures while cleaning up.
}
}
if (kind == Images.Thumbnails.MICRO_KIND && bitmap != null) {
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
}
return bitmap;
}


之后再自己的后台线程中调用该方法得到网络视频的缩略图bitmap然后在主线程中调用imageView.setImageBitmap(bitmap)即可;


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux 程序设计学习笔记----Linux.. 下一篇Android程序 依赖库引用Gson 报ja..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: