0

目前正在开发一个从 SD 卡加载图像的应用程序。在模拟器中,以下工作正常并找到要显示的图像。但是,在 Galaxy S2 和 S3 上 - 从未显示任何图像。有任何想法吗?

Cursor cursor = getContentResolver().query(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
    { MediaStore.Images.Thumbnails._ID }, 
    null, 
    null, 
    MediaStore.Images.Media._ID
);

int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int size        = cursor.getCount();
int imageId     = 0;

for (int i = 0; i < size; i++) {
     if(cursor.isClosed()) {
         continue;
     }
     cursor.moveToPosition(i);
     imageId = cursor.getInt(columnIndex);
     uri     = Uri.withAppendedPath(
         MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, 
         Integer.toString(imageId)
     );

try {
    bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

        if (bitmap != null) {
            newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70,true);
            bitmap.recycle();

            if (newBitmap != null) {
                publishProgress(newBitmap);
            }
        }
} catch (IOException e) {}
}
cursor.close();
return null;
4

1 回答 1

0

在设备上测试后发现以下问题解决了:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.outHeight = 70;
    options.outWidth = 70;
    Bitmap bitmap = null;

    Cursor cursor = getContentResolver().query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
        PROJECTION, 
        null, 
        null, 
        MediaStore.Images.Media._ID
    );
    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
    int size        = cursor.getCount();

    for (int i = 0; i < size; i++) {
        if(cursor.isClosed()) {
            break;
        }
        cursor.moveToPosition(i);

        bitmap = MediaStore.Images.Thumbnails.getThumbnail(
            getContentResolver(), 
            cursor.getInt(columnIndex), 
            MediaStore.Images.Thumbnails.MICRO_KIND,  
            options
        );
        publishProgress(new LoadedImage(bitmap));
    }
    cursor.close();
于 2012-11-29T21:21:50.750 回答