Android画廊:Bitmap Images会抛出arrayIndexOutOfBoundException

时间:2012-09-11 12:41:08

标签: android bitmap gallery

以下是我的代码:

String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cur = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] {ImageColumns._ID}, null, null,null);
if ((cur != null) && cur.moveToFirst()) {
    //  Integer idsArray[] = new Integer[cur.getCount()];
    for (int i = 0; i < cur.getCount(); i++) {
        idsArray[i] = cur.getInt(cur.getColumnIndex(ImageColumns._ID));


        cur.moveToNext();
    }

}
Bitmap originalImage = BitmapFactory.decodeFile(cur.getString(cur.getColumnIndex(filePathColumn[0])));
int width = originalImage.getWidth();

int height = originalImage.getHeight();

cur.close();

我想获得每张图片的高度和宽度。我想使用getHeight()getWidth()方法,但上面的代码给了我一个错误。请帮帮我。

1 个答案:

答案 0 :(得分:0)

检查

Cursor cur = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, **new String[] {ImageColumns._ID}**, null, null,null);

您只查询ImageColumns._ID字段。添加MediaStore.Images.Media.DATA即可。

像这样:

    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursor cur = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[] { ImageColumns._ID, MediaStore.Images.Media.DATA }, null, null, null);

    if ((cur != null) && cur.moveToFirst()) {
        //  Integer idsArray[] = new Integer[cur.getCount()];
        for (int i = 0; i < cur.getCount(); i++) {
            cur.getInt(cur.getColumnIndex(ImageColumns._ID));

            Bitmap originalImage = BitmapFactory.decodeFile(cur.getString(cur.getColumnIndex(filePathColumn[0])));
            int width = originalImage.getWidth();
            int height = originalImage.getHeight();

            cur.moveToNext();
        }

    }

    cur.close();