Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference

后端 未结 6 655
忘了有多久
忘了有多久 2021-02-07 01:46

I have BitmapScalingHelper.java:

public class BitmapScalingHelper
{
    public static Bitmap decodeResource(Resources res, int resId, int dstWidth, int dstHeigh         


        
相关标签:
6条回答
  • 2021-02-07 02:13

    Simple answer : follow following steps..

    1. press long click on installed APP icon.
    2. Go to the App info section.
    3. now click on app permission section
    4. enable storage permission

    now it's working properly

    enter image description here

    enter image description here

    0 讨论(0)
  • 2021-02-07 02:14

    This error can also be caused if you use a poorly created library jar, and have minifyEnabled set to true on your gradle.

    0 讨论(0)
  • 2021-02-07 02:15

    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 -

    1. No read permission
    2. The image file is corrupt
    3. There is not enough memory to decode the file
    4. The resource does not exist
    5. Invalid options specified in the options variable.

    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;
        }
    }
    
    0 讨论(0)
  • 2021-02-07 02:15

    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

    0 讨论(0)
  • 2021-02-07 02:22

    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.

    0 讨论(0)
  • 2021-02-07 02:33

    There are some reasons may cause the error.

    1. Lack of the Permission in AndroidManifest.xml. Read or write permission.
    2. The file doesn't exist or you are trying to access to a file that could't be decoded as a bitmap.

    From the code you post,I can't find any logic error.Here are my suggestions:

    1. Change another image and test.

    2. Try to use ImageLoader Library like Glide or Fresco and test.

    0 讨论(0)
提交回复
热议问题