考虑如下图所示的场景:
三张照片,其中一张是大型GIF文件(3MP)。
我正在查询MediaStore以检索相应的缩略图。如果我通过CursorLoader使用此sortOrder初始化Cursor:
MediaStore.Images.Media.DATE_ADDED + " DESC""
会发生什么: MediaStore会返回先前成功检索到的缩略图:
预期的行为当MediaStore由于某种原因无法检索给定图像的缩略图时,它必须返回NULL,根据其Javadoc:“...返回一个Bitmap实例。它如果与origId关联的原始图像不存在或内存不足,则可能为null。“
如果我用这个sortOrder初始化Cursor:
MediaStore.Images.Media.DATE_ADDED + " ASC""
运行得很好:
但是我不能简单地改变sortOrder,因为要求是先显示最新的图片。
以下是我的示例代码和here is the complete sample project以及the three images used to reproduce。
package com.example.getimagefrommediastore;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.widget.ImageView;
import android.widget.TextView;
public class GetThumbnailsFromMediaStoreSampleActivity extends Activity {
TextView mThumb_id_01;
TextView mThumb_id_02;
TextView mThumb_id_03;
ImageView mImg_01;
ImageView mImg_02;
ImageView mImg_03;
boolean isThumb01 = true; // Simple flag to control this example
boolean isThumb02 = true;
Cursor mCursorLoader;
int mColumnIndex;
long mOrigId; // Original image id associated with thumbnail of interest
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Just initializing views
mThumb_id_01 = (TextView) findViewById(R.id.thumb_id_01);
mThumb_id_02 = (TextView) findViewById(R.id.thumb_id_02);
mThumb_id_03 = (TextView) findViewById(R.id.thumb_id_03);
mImg_01 = (ImageView) findViewById(R.id.thumb_01);
mImg_02 = (ImageView) findViewById(R.id.thumb_02);
mImg_03 = (ImageView) findViewById(R.id.thumb_03);
// Initializing CursorLoader
mCursorLoader = initializeCursorLoader();
mColumnIndex = mCursorLoader.getColumnIndex(MediaStore.Images.Media._ID);
// Go thru all the images in the device (EXTERNAL_CONTENT_URI)
// In this example there are only three images
for (int i = 0; i < mCursorLoader.getCount(); i++) {
mCursorLoader.moveToPosition(i);
mOrigId = mCursorLoader.getInt(mColumnIndex);
// Update views
chooseViewToUpdate();
}
}
private Cursor initializeCursorLoader() {
String[] COLUMNS = {
MediaStore.Images.Thumbnails._ID, MediaStore.Images.Media.DATA
};
CursorLoader cursorLoader = new CursorLoader(
GetThumbnailsFromMediaStoreSampleActivity.this, // Context
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // Uri
COLUMNS, // Projection
null, // Selection
null, // Selection Args
// Sort Order: DESC = newest first
// Sort Order: ASC = oldest first
MediaStore.Images.Media.DATE_ADDED + " DESC");
// *** NOTE ***
// With:
//
// MediaStore.Images.Media.DATE_ADDED + " ASC"
//
// It runs just fine (MediaStore returns 'null' for invalid thumbnails)
// The problem seems to reside on the " DESC" tag.
//
// How bizarre is that?
return cursorLoader.loadInBackground();
}
private void chooseViewToUpdate() {
if (isThumb01) {
updateUI(mThumb_id_01, mImg_01);
isThumb01 = false;
} else if (isThumb02) {
updateUI(mThumb_id_02, mImg_02);
isThumb02 = false;
} else {
updateUI(mThumb_id_03, mImg_03);
}
}
private void updateUI(TextView textView, ImageView imgView) {
textView.setText("ID:" + String.valueOf(mOrigId));
Bitmap mediaStoreThumbmail = MediaStore.Images.Thumbnails.getThumbnail(
this.getContentResolver(),
mOrigId,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
if (mediaStoreThumbmail != null) {
imgView.setImageBitmap(mediaStoreThumbmail);
}
}
我错过了什么吗?有没有人知道可能有什么问题?
I filled a bug against Android anyway
修改
似乎这个问题是fixed in Lollipop.(该主题的最后一条评论)。
答案 0 :(得分:2)
我只是在这里猜测。当你要求MICRO_KIND时,操作系统正在创建一个新的图像,它在DESC光标上再次生成相同的图像。
一种解决方法是为图像ID加载ArrayList。然后在ArrayList中使用缩略图。
可能使用MINI_KIND和bmoptions.inSampleSize = 2尝试您的代码;
final BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize =2;
Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(
context.getContentResolver(), newImageId,
MediaStore.Images.Thumbnails.MINI_KIND,
bmOptions);
答案 1 :(得分:0)
三年后,似乎这个问题is fixed in Lollipop。 (该线程的最后评论) 因此,我正在回答我自己的问题。