我正在学习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为空。
图像为位图格式。
我的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));

谢谢!

最佳答案

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

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

关于android - BitmapFactory.decodeResource使用inJustDecodeBounds = true为outHeight和outWidth返回0,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18716531/

10-10 09:11