BitmapFactory.Options gives 0 width and height

final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); final int height = options.outHeight; final int width = options.outWidth; 

path is the path to the image file that is correct.

The problem is options.outHeight and options.outWidth = 0 when the image is captured in Landscape mode from Autorotate to . If I turn off AutoRotate , it works fine. Since its width and height are 0, I got a zero bitmap at the end.

Full code:

 Bitmap photo = decodeSampledBitmapFromFile(filePath, DESIRED_WIDTH, DESIRED_HEIGHT); public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); // Calculate inSampleSize, Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; options.inPreferredConfig = Bitmap.Config.ARGB_8888; int inSampleSize = 1; if (height > reqHeight) { inSampleSize = Math.round((float) height / (float) reqHeight); } int expectedWidth = width / inSampleSize; if (expectedWidth > reqWidth) { inSampleSize = Math.round((float) width / (float) reqWidth); } options.inSampleSize = inSampleSize; // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(path, options); } 
+7
android bitmap
source share
1 answer

I had the same problem, I fixed it by changing:

 BitmapFactory.decodeFile(path, options); 

in

 try { InputStream in = getContentResolver().openInputStream( Uri.parse(path)); BitmapFactory.decodeStream(in, null, options); } catch (FileNotFoundException e) { // do something } 

After this change, the width and height were set correctly.

+6
source share

All Articles