以下是我的代码:
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()
方法,但上面的代码给了我一个错误。请帮帮我。
答案 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();