记录一下:
1.使用MediaMetadataRetriever获取Bitmap:
较占内存
MediaMetadataRetriever media = new MediaMetadataRetriever();
media.setDataSource(videoPath);
Bitmap bitmap = media.getFrameAtTime();
2.使用ThumbnailUtils类获取Bitmap:
需要Android 2.2以后,也较占内存
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, Thumbnails.MICRO_KIND);
//Bitmap bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
3.从媒体库中查询:
不是很稳定而且有新视频的时候要通知系统重新扫描
Cursor cursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Video.Media.ALBUM + "=?", new String[]{Constants.DIRECTORY_VIDEO}, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
try {
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
//视频缩略图路径
String albumPath = "";
Cursor thumbCursor = context.getApplicationContext().getContentResolver().query(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
null, MediaStore.Video.Thumbnails.VIDEO_ID
+ "=" + id, null, null);
if (thumbCursor.moveToFirst()) {
albumPath = thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Video.Thumbnails.DATA));
Bitmap bitmap = BitmapFactory.decodeFile(albumPath);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null)
cursor.close();
}
4.使用开源库Fresco
mImage.setImageURI(Uri.parse("file://" + filePath));
5.使用开源库Glide
Glide
.with( context )
.load( Uri.fromFile( new File( filePath ) ) )
.into( mImage);
作者:哎呦哥哥QAQ
链接:https://www.jianshu.com/p/4a519531321c
來源:简书