0

im trying to get imageview from drawable and resize it my current code is working on all devices except on oppo devices it crash inside crop() method

the crash im getting is on the first line in crop() method you can find below

Caused by: java.lang.NullPointerException: 

my current code is:

public Bitmap getTileBitmap(int id, int size) {
    String string = tileUrls.get(id);
    if (string.contains(Themes.URI_DRAWABLE)) {
        String drawableResourceName = string.substring(Themes.URI_DRAWABLE.length());
        int drawableResourceId = Shared.context.getResources().getIdentifier(drawableResourceName, "drawable", Shared.context.getPackageName());
        Bitmap bitmap = Utils.scaleDown(drawableResourceId, size, size);
        return Utils.crop(bitmap, size, size);

    }
    return null;
}


public static Bitmap crop(Bitmap source, int newHeight, int newWidth) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();
    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale, yScale);
    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;
    float left = (newWidth - scaledWidth) / 2;
    float top = (newHeight - scaledHeight) / 2;
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
    Canvas canvas = new Canvas(dest);
    canvas.drawBitmap(source, null, targetRect, null);

    return dest;
}

EDIT: added scale method which may cause the issue

public static Bitmap scaleDown(int resource, int reqWidth, int reqHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(Shared.context.getResources(), resource);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(Shared.context.getResources(), resource, options);
}

and so far im not sure what is causing the crash only on oppo devices

Hossam Hassan
  • 795
  • 2
  • 13
  • 39
  • Presumably `Utils.scaleDown()` is returning `null`. – CommonsWare Feb 08 '21 at 12:04
  • You will need to inspect `Utils.scaleDown()` and debug why it returns `null` on Oppo devices. – c0delama Feb 08 '21 at 12:04
  • i have updated the question – Hossam Hassan Feb 08 '21 at 12:06
  • im not able to debug because the device i have and my emulator are not crashing but im getting crash on one of the users phone – Hossam Hassan Feb 08 '21 at 12:12
  • But you can add a check for bitmap==null and display a toast and stop/return instead of letting your app crash. Also display inSampleSize then and resolution. – blackapps Feb 08 '21 at 12:27
  • i have added if (bitmap == null) { return null; } else { return Utils.crop(bitmap, size, size); } but when i send apk to the user to test it on his phone he is getting all the images empty, but the crash has stopped now need to find out what is causing the null bitmap on oppo device – Hossam Hassan Feb 08 '21 at 12:37
  • It is just a shot in the dark, but you could try to use the `getApplicationContext()` instead of (or for your) `Shared.context`. It could easily be that the context that you are using is not available on these specific devices. – c0delama Feb 08 '21 at 12:46

1 Answers1

0

Try this:

options.inJustDecodeBounds = true;
options.inScaled = false;
options.inDensity = 0;
options.inMutable = true; //API 11. Pass to canvas? Might crash without this.
//Load the image here... BitmapFactory.decodeResource()...

... //Some calculations.

options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Darkman
  • 2,941
  • 2
  • 9
  • 14