Android之通過ContentResolver獲取手機圖片和視訊的路徑和生成縮圖和縮圖路徑
阿新 • • 發佈:2018-11-06
1 問題
獲取手機所有圖片和視訊的路徑和生成圖片和視訊的縮圖和縮圖路徑
生成縮圖我們用的系統函式
public static Bitmap getThumbnail(ContentResolver cr, long origId, int kind, Options options) {
throw new RuntimeException("Stub!");
}
呼叫如下
MediaStore.Images.Thumbnails.getThumbnail
2 獲取手機所有視訊測試程式碼
/** *get system orignal Thumbnail **/ private String getVideoSystemThumbnail(Content content, String id) { String thumbnail = ""; ContentResolver cr = content.getContentResolver(); Cursor cursor = cr.query( MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI, new String[]{ MediaStore.Video.Thumbnails.DATA }, MediaStore.Video.Thumbnails.VIDEO_ID + "=" + id, null, null); if (cursor != null && cursor.moveToFirst()) { thumbnail = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA)); cursor.close(); } return thumbnail; } /** *if orignal do not Thumbnail, we will create Thumbnail */ public String getVedioThumbnail(Content content, Cursor cursor, String path, String id) { String thumbnailPath = getVideoSystemThumbnail(content, id); Log.d(TAG, "getVedioThumbnail thumbnailPath is:" + thumbnailPath); if (thumbnailPath == null) { //create Thumbnail MediaStore.Images.Thumbnails.getThumbnail(content.getContentResolver(), cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media._ID)), MediaStore.Video.Thumbnails.MICRO_KIND, null); thumbnailPath = getVideoSystemThumbnail(content, id); return thumbnailPath == null ? path : thumbnailPath; } else { return thumbnailPath; } } public void showAllViedio(Content content) { Uri targetUrl = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; Cursor vidioCursor = content.getContentResolver().query(targetUrl, new String[]{ MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID}, null, null, MediaStore.Video.Media.DATE_MODIFIED + " DESC"); String filePath = ""; String fileType = ""; string ThumbnailPath = ""; int fileSize; if (vidioCursor != null) { while (vidioCursor.moveToNext()) { filePath = vidioCursor.getString(vidioCursor.getColumnIndex(MediaStore.Video.Media.DATA)); Log.i(TAG, "filePath is:" + filePath); fileType = fullPath.substring(0, filePath.lastIndexOf("/")); Log.i(TAG, "fileType is:" + fileType); file = new File(fullPath); if (!file.exists()) { continue; } //we can get ThumbnailPath ThumbnailPath = getVedioThumbnail(content, vidioCursor, fullPath, vidioCursor.getString(vidioCursor.getColumnIndex(MediaStore.Video.Media._ID))); Log.i(TAG, "ThumbnailPath is:" + ThumbnailPath); fileSize = file.length(); Log.i(TAG, "fileSize is:" + fileSize); } } }
3 如何獲取手機所有圖片
把上面的Video改成Images,然後就ok了