需要快速提取圖片和視頻縮略圖可以直接訪問 android.provider.MediaStore.Images.Thumbnails 和android.provider.MediaStore.Video.Thumbnails這兩個數據庫,即可查詢出來縮略圖 。
如何判斷文件呢? 可以通過Cursor遍歷數據庫,對比INTERNAL_CONTENT_URI字段的值,這是一個Uri,這裡保存著Android手機SD卡上的多媒體文件完整路徑。
Uri originalUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
//若為視頻則為MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
ContentResolver cr = this.getContentResolver();
Cursor cursor = cr.query(originalUri, null, null, null, null);
if (cursor == null) {
return;
}
for (cursor.moveToFirst();!cursor.isAfterLast(); cursor.moveToNext()) {
long thumbNailsId = cursor.getLong(cursor .getColumnIndex("_ID"));
Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(cr,
thumbNailsId, Video.Thumbnails.MICRO_KIND, null);
//若為視頻則為
// Bitmap bitmap = MediaStore.Video.Thumbnails.getThumbnail(cr,
// thumbNailsId, Video.Thumbnails.MICRO_KIND, null);
return bitmap;
}