考虑场景在这样的画面:
3张照片,其中之一是一个大的GIF文件(3MP)。
我为了检索对应的缩略图查询MediaStore。 如果我通过这种中将sortOrder CursorLoader初始化光标:
MediaStore.Images.Media.DATE_ADDED + " DESC""
会发生什么:MediaStore返回以前的成功检索缩略图:
预期的行为:当MediaStore无法检索出于某种原因必须返回NULL,按照它的Javadoc给定的图像的缩略图:” ......返回Bitmap实例它可以为空,如果有origId相关的原始图像不存在或内存是不够的。”
如果我初始化这个中将sortOrder光标:
MediaStore.Images.Media.DATE_ADDED + " ASC""
它运行得很好:
不过,我不能简单地改变中将sortOrder因为要求是最先显示最新图片。
下面是我的示例代码和下面是完整的示例项目 ,以及用于再现三个图像 。
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);
}
}
我缺少的东西吗? 有没有人有一个想法是什么可能是错误的?
我充满对Android的一个bug呢 。
编辑
看来,这个问题是固定在棒棒糖。 (该线程的最后注释)。