I have BitmapScalingHelper.java:
public class BitmapScalingHelper
{
public static Bitmap decodeResource(Resources res, int resId, int dstWidth, int dstHeigh
Simple answer : follow following steps..
now it's working properly
enter image description here
enter image description here
This error can also be caused if you use a poorly created library jar, and have minifyEnabled set to true on your gradle.
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;
}
}
Your are using: Bitmap decodeFile (String pathName)
This method may return null if the decode of file fail.
I think that it can be related to permission issue on some device or to image format not supported.
If you are using GIF, try https://developer.android.com/reference/android/graphics/Movie.html
Well, I got this error repeatedly, and my fix for this problem is to make sure you use a .png image as the bitmap image and save it in the drawable folder. Previously I was using it as a vector asset. This worked fine for me.
There are some reasons may cause the error.
From the code you post,I can't find any logic error.Here are my suggestions:
Change another image and test.
Try to use ImageLoader Library like Glide or Fresco and test.