Android我爱编程work

Android获取本地视频缩略图的几种方法

2018-04-13  本文已影响623人  哎呦哥哥QAQ

记录一下:

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);

参考:

android获得本地视频缩略图
安卓获取视频缩略图,展示于ListView中,完美实现
android 获取本地视频文件以及缩略图
查询手机所有视频文件以及获取缩略图

上一篇下一篇

猜你喜欢

热点阅读