Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 取得影片與圖片縮圖的具體實作

編輯:關於 Android 程式設計

一、影片縮圖

取得影片縮圖有兩條路:一是透過 MediaStore 內容提供器,二是自己動手產生。

做法一:走 MediaStore

用 ContentResolver 以檔案路徑反查這支影片在媒體庫中的 _ID,再把 ID 交給 MediaStore.Video.Thumbnails.getThumbnail 取回縮圖。這裡刻意把 preferredConfig 設為 RGB_565 並關掉 dither,可以省下不少記憶體。

缺點很明顯:必須等媒體庫掃描更新後,才看得到最新影片的縮圖。剛下載或剛錄製的檔案往往查不到,這時候就得退回做法二。

/**
 * 透過內容提供器取得影片縮圖
 */
public static Bitmap getVideoThumbnail(Context context, ContentResolver cr, String Videopath) {
    ContentResolver testcr = context.getContentResolver();
    String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID, };
    String whereClause = MediaStore.Video.Media.DATA + " = '" + Videopath + "'";
    Cursor cursor = testcr.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            projection, whereClause, null, null);
    int _id = 0;
    String videoPath = "";
    if (cursor == null || cursor.getCount() == 0) {
        return null;
    }
    if (cursor.moveToFirst()) {
        int _idColumn = cursor.getColumnIndex(MediaStore.Video.Media._ID);
        int _dataColumn = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
        do {
            _id = cursor.getInt(_idColumn);
            videoPath = cursor.getString(_dataColumn);
        } while (cursor.moveToNext());
    }
    cursor.close();

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inDither = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr, _id,
            Images.Thumbnails.MINI_KIND, options);
    return bitmap;
}

做法二:自行擷取

用 ThumbnailUtils 直接向檔案解碼取幀,再依目標尺寸裁切縮放。這個做法比較耗時,但好處是不依賴媒體庫狀態,任何路徑都能處理。

/**
 * 自行產生影片縮圖
 */
private Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind) {
    Bitmap bitmap = null;
    bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
    bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
            ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
    return bitmap;
}

兩段式呼叫的用意是:先解一張原始尺寸的幀,再用 extractThumbnail 縮到需要的邊長,避免一次配置過大的 Bitmap。OPTIONS_RECYCLE_INPUT 會在縮放完成後回收中間那張原圖,降低記憶體峰值。

二、圖片縮圖

圖片的做法與影片幾乎一致,只是把 MediaStore 的表換成 Images:

/**
 * 取得圖片縮圖
 */
public static Bitmap getImageThumbnail(Context context, ContentResolver cr, String Imagepath) {
    ContentResolver testcr = context.getContentResolver();
    String[] projection = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, };
    String whereClause = MediaStore.Images.Media.DATA + " = '" + Imagepath + "'";
    Cursor cursor = testcr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            projection, whereClause, null, null);
    int _id = 0;
    String imagePath = "";
    if (cursor == null || cursor.getCount() == 0) {
        return null;
    }
    if (cursor.moveToFirst()) {
        int _idColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID);
        int _dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
        do {
            _id = cursor.getInt(_idColumn);
            imagePath = cursor.getString(_dataColumn);
        } while (cursor.moveToNext());
    }
    cursor.close();

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inDither = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(cr, _id,
            Images.Thumbnails.MINI_KIND, options);
    return bitmap;
}

實務上的幾個提醒

第一,查詢條件用字串拼接路徑並不安全,路徑中若含單引號就會出錯,正式專案應改用 query(uri, projection, where, new String[]{path}, null) 的參數化寫法。

第二,MINI_KIND 固定回傳約 512×384 的縮圖,若介面需要的是小圖示,這樣的大小其實偏重,可考慮自行用 BitmapFactory 的 inJustDecodeBounds 搭配 inSampleSize 精準降採樣。

第三,MediaStore 的縮圖由系統統一快取管理,重複取用不會重複解碼;但自行呼叫 ThumbnailUtils 就沒有這層好處,若清單中會大量重複顯示同一支影片,建議自己做一層記憶體快取(例如 LruCache),並在 Activity 銷毀時清空。

最後,Cursor 一定要關。上面範例已用 close() 處理,實作時更推薦改用 try-with-resources 或 Android 的 useCursor 機制,避免例外發生時游標洩漏。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved