Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android BitmapFactory always returns 0

I am using this widely know code

Display display = this.getWindowManager().getDefaultDisplay();
            float dw = display.getWidth();
            float dh = display.getHeight();

            // load image dimensions, not the image itself
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bmp = BitmapFactory.decodeFile(MyApp.getImageFilePath());


            int heightRatio = (int) FloatMath.ceil(bmpFactoryOptions.outHeight / dh);
            int widthRatio = (int) FloatMath.ceil(bmpFactoryOptions.outWidth / dw);

            if ((heightRatio > 1) && (widthRatio > 1))// if true one side of the image is bigger than the screen
            {
                if (heightRatio > widthRatio) {
                    bmpFactoryOptions.inSampleSize = heightRatio;
                } else {
                    bmpFactoryOptions.inSampleSize = widthRatio;
                }
            }
            // decode it for real
            bmpFactoryOptions.inSampleSize = bmpFactoryOptions.inSampleSize;
            bmpFactoryOptions.inPreferredConfig = Bitmap.Config.ARGB_8888; //http://www.curious-creature.org/2010/12/08/bitmap-quality-banding-and-dithering/
            bmpFactoryOptions.inJustDecodeBounds = false;
            bmpFactoryOptions.inDither = true;
            bmp = BitmapFactory.decodeFile(MyApp.getImageFilePath(), bmpFactoryOptions);

            ImageView photo = (ImageView) this.findViewById(R.id.imageView1);

the problem is that

bmpFactoryOptions.outWidth
bmpFactoryOptions.outHeight
bmpFactoryOptions.inSampleSize

always have the value 0. I have tested it on three different devices, what am i doing wrong ?

and setting bmpFactoryOptions.inSampleSize = bmpFactoryOptions.inSampleSize + 1;

has no effect

like image 305
max4ever Avatar asked May 23 '26 08:05

max4ever


1 Answers

Contrary to what the title says, your BitmapFactory is returning a Bitmap just fine.

You never assigned your bmpFactoryOptions handle in this statement. So querying it should produce no results. Incrementing it doesn't help because you never initialized it to be the properties of an image.

Bitmap bmp = BitmapFactory.decodeFile(MyApp.getImageFilePath());

Do something like this instead:

Bitmap bmp = BitmapFactory.decodeFile(MyApp.getImageFilePath(), bmpFactoryOptions);

Which will attach your bmpFactoryOptions to your bmp. Then you can query the options. Note that your BitmapFactory is returning a perfectly good bitmap, just a bitmap where you don't have any handle to its properties.

like image 175
Eric Leschinski Avatar answered May 24 '26 20:05

Eric Leschinski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!