BitmapFactory.decodeResource is returning 0 for outHeight & outWidth with inJustDecodeBounds = true

StackOverflow https://stackoverflow.com/questions/18716531

  •  28-06-2022
  •  | 
  •  

Question

I am following the android developer tutorials on displaying bitmap's efficiently and I'm trying to read a bitmap's dimensions and type.

This is the code I have for reading the bitmap:

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;

My results for imageHeight, imageWidth both seem to be 0 and the imageType is null.

The image is of bitmap format.

My onCreate method is

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));

Thanks!

Was it helpful?

Solution

Change this line - image = BitmapFactory.decodeResource(context.getResources(), R.id.myimage, options);

to

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

OTHER TIPS

R.id.myimage is not a valid drawable resource. Should be R.drawable.something.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top