I have BitmapScalingHelper.java:
public class BitmapScalingHelper
{
public static Bitmap decodeResource(Resources res, int resId, int dstWidth, int dstHeigh
The problem you are facing is that you are trying to getWidth()
on your unscaledBitmap
in the createScaledBitmap
function. Clearly, your unscaledBitmap
is null
sometimes; and calling getWidth()
is causing the Null Pointer exception.
The root cause is that decodeResource
is returning you a null for whatever reason.
The reasons can include -
I'd suggest that you modify your code to include a null-check on the decoded bitmap, log it and debug from there on the specific devices that you see the error occurring.
It may also be that your options variable that you are re-using is being interpreted differently in the second call to decodeResource
. You might try passing a null there.
The modified code should be as follows -
public class BitmapScalingHelper
{
public static Bitmap decodeResource(Resources res, int resId, int dstWidth, int dstHeight)
{
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inJustDecodeBounds = false;
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, dstWidth,
dstHeight);
options = new Options();
//May use null here as well. The funciton may interpret the pre-used options variable in ways hard to tell.
Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resId, options);
if(unscaledBitmap == null)
{
Log.e("ERR","Failed to decode resource - " + resId + " " + res.toString());
return null;
}
return unscaledBitmap;
}
}