Android獲取本地視頻縮略圖的幾種方法(轉)

記錄一下:ide

1.使用MediaMetadataRetriever獲取Bitmap:
較佔內存code

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.使用開源庫Frescoget

mImage.setImageURI(Uri.parse("file://" +  filePath));

5.使用開源庫Glideit

Glide  
.with( context )
.load( Uri.fromFile( new File( filePath ) ) )
.into( mImage);



做者:哎呦哥哥QAQ
連接:https://www.jianshu.com/p/4a519531321c
來源:簡書
 io

相關文章
相關標籤/搜索