2

I have been trying to follow this answer https://stackoverflow.com/a/8527745/1352702 , to create a scaled bitmap instead of just drawing a bitmap on the canvas, to solve some memory issues.

Here is my code,

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable = true;
options.inSampleSize = 8;

Bitmap s000 = BitmapFactory.decodeResource(getResources(), getResources().getIdentifier("zoo" + i, "drawable", getPackageName()));
int X = c.getWidth()/2 - s000.getWidth()/2 ;
int Y = c.getHeight()/2 - s000.getHeight()/2 ;
Bitmap.createScaledBitmap(s000, s000.getWidth(), s000.getHeight(), true);

But it just creates a blank screen. Also how to pass the position parameters of the image (X and Y respectively) in the createScaledBitmap method ?

Community
  • 1
  • 1
Tomarinator
  • 782
  • 1
  • 11
  • 28

1 Answers1

1

For the record, I had the same issue when trying to resize certain hi-resolution grayscale PNGs : they were properly loaded by BitmapFactory.decodeResource, then Bitmap.createScaledBitmap turned them into black pictures.

Turns out that, on API 26+, Android doesn't know how to handle bitmaps whose ColorSpace is not properly detected ("Unknown"), and messes up the resizing.

The working solution for my app was to define a preferred ColorSpace at load time :

BitmapFactory.Options options = new BitmapFactory.Options();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            options.inPreferredColorSpace = ColorSpace.get(ColorSpace.Named.SRGB);
bitmap = BitmapFactory.decodeXXX(<whatever method you fancy>, options);
Paul W
  • 149
  • 1
  • 6