我試圖提供一個應用內活動,它在 設備的媒體商店中顯示照片的縮略圖,並允許用戶選擇一個。在用戶進行 選擇後,應用程序將讀取原始的全尺寸圖像並執行相關操作。如何查詢Android MediaStore Content Provider,避免孤立圖像?
我使用以下代碼來創建一個Cursor
超過上的外部存儲 所有圖像:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_select);
mGridView = (GridView) findViewById(R.id.image_select_grid);
// Query for all images on external storage
String[] projection = { MediaStore.Images.Media._ID };
String selection = "";
String [] selectionArgs = null;
mImageCursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, selection, selectionArgs, null);
// Initialize an adapter to display images in grid
if (mImageCursor != null) {
mImageCursor.moveToFirst();
mAdapter = new LazyCursorAdapter(this, mImageCursor, R.drawable.image_select_default);
mGridView.setAdapter(mAdapter);
} else {
Log.i(TAG, "System media store is empty.");
}
}
而下面的代碼來加載縮略圖(的Android 2.x的代碼示出) :
public Bitmap loadFullImage(Context context, Uri photoUri ) {
Cursor photoCursor = null;
try {
// Attempt to fetch asset filename for image
String[] projection = { MediaStore.Images.Media.DATA };
photoCursor = context.getContentResolver().query(photoUri,
projection, null, null, null);
if (photoCursor != null && photoCursor.getCount() == 1) {
photoCursor.moveToFirst();
String photoFilePath = photoCursor.getString(
photoCursor.getColumnIndex(MediaStore.Images.Media.DATA));
// Load image from path
return BitmapFactory.decodeFile(photoFilePath, null);
}
} finally {
if (photoCursor != null) {
photoCursor.close();
}
}
return null;
}
:
// ...
// Build URI to the main image from the cursor
int imageID = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
Integer.toString(imageID));
loadThumbnailImage(uri.toString());
// ...
protected Bitmap loadThumbnailImage(String url) {
// Get original image ID
int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
// Get (or create upon demand) the micro thumbnail for the original image.
return MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(),
originalImageId, MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
和下面的代碼,一旦用戶做出選擇,以加載來自URL的原始圖像
我在包括我自己的個人電話在內的一些Android設備上看到的問題是,我在onCreate()
的查詢中得到的 光標包含幾個實際完整大小的圖像文件(JPG或PNG)不見了。 (在我的手機中,圖像已被導入並隨後由iPhoto擦除)。
孤立條目可能有也可能沒有縮略圖,具體取決於在AWOL時是否在實際媒體文件之前生成的縮略圖。最終的結果是,應用程序會顯示實際不存在的圖像的縮略圖。
我有幾個問題:
- 有一個查詢我可以對
MediaStore
內容提供商將篩選出 圖像與在返回Cursor
缺少的媒體? - 是否有方法或API來強制
MediaStore
重新掃描並消除孤立條目?在我的手機上,我安裝了USB,然後卸下外部媒體,這應該會觸發重新掃描。但孤兒條目仍然存在。 - 或者是我的方法導致這個問題有什麼根本錯誤?
謝謝。