BitmapFactory.decodeResource为outHeight& outWidth with inJustDecodeBounds = true

时间:2013-09-10 10:30:06

标签: android bitmapfactory

我正在关注有关显示位图的android开发人员教程,我正在尝试读取位图的尺寸和类型。

这是我读取位图的代码:

BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    image = BitmapFactory.decodeResource(context.getResources(), R.id.myimage, options);
    imageHeight = options.outHeight;
    imageWidth = options.outWidth;
    imageType = options.outMimeType;

我对imageHeight,imageWidth的结果似乎都是0,而imageType是null。

图像是位图格式。

我的onCreate方法是

ImageView imgView = (ImageView) findViewById(R.id.myimage);
    Drawable draw = getResources().getDrawable(R.drawable.bmp_login_screen);
    imgView.setImageDrawable(draw);



    TextView textView = (TextView) findViewById(R.id.text_view);

    ReadBitmapDimen dimensions = new ReadBitmapDimen(getApplicationContext());
    textView.setText(" Dimensions for userlogin image: outHeight = " + String.valueOf(dimensions.imageHeight));

谢谢!

2 个答案:

答案 0 :(得分:2)

更改此行 - image = BitmapFactory.decodeResource(context.getResources(), R.id.myimage, options);

image = BitmapFactory.decodeResource(context.getResources(), R.drawable.bmp_login_screen, options);

答案 1 :(得分:1)

R.id.myimage不是有效的可绘制资源。应该是R.drawable.something