編輯:關於 Android 程式設計
取得影片縮圖有兩條路:一是透過 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 機制,避免例外發生時游標洩漏。
開發隨筆:ListView 自定義、SQLite 升級與 Activity 傳值實踐
開發隨筆:從列表頁到詳情頁的實作整理 在這次的小專案中,我完成了列表頁與詳情頁的資料流串接,過程中也重新梳理了 SQLite 升級、資料庫查詢、Activity 間傳值,
Android 系統自帶 ActionBar 用法總結
Android 不同版本對 ActionBar 的支援存在差異,本文將常見用法整理如下。 一、在 support.v7 套件中 引入選單時,使用 onCreateOpti
Android 反射機制實戰:用 Class.forName 動態呼叫方法做計算機
本文以一個加減乘除計算機為例,示範如何在 Android 中用 Java 反射機制動態取得類別、建立實例並呼叫方法。 先搞懂什麼是反射 Java 反射機制的定義是:在運
怎麼查手機型號?從背蓋、包裝到系統設定的三個方法
想查教學、找韌體、送修報價,第一步都是同一件事:確認自己這支手機的確切型號。型號不對,下載的檔案裝不上、查到的教學也不適用。以下整理三個查找位置,由實體到系統。如何快速找