-1

I am getting a NullPointerException. Why?

private Bitmap changeDimens(int height,int width) {
        Bitmap bitmap=((BitmapDrawable)modified.getDrawable()).getBitmap().copy(Bitmap.Config.ARGB_8888,true);
        Matrix matrix = new Matrix();
        matrix.postScale(width, height);
        Bitmap bitmapEscaled=Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
        //bitmap.recycle();
        //Bitmap bitmapEscaled=Bitmap.createScaledBitmap(bitmap,width,height,true);
        return bitmapEscaled;
    }

I am trying to get a bitmap with the dimensions given by parameters and getting a NullPointer in this line:

Bitmap bitmapEscaled=Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false); 

This in my log:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.Bitmap.setHasAlpha(boolean)' on a null object reference
Jacob Sánchez
  • 390
  • 3
  • 15
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – SiKing Jan 09 '19 at 23:09

2 Answers2

0

Not sure if you need to create a bitmap copy in a such way. You can try this snippet, if it suits to your needs:

public static Bitmap getResizedBitmap(Bitmap source, int toWidth, int toHeight) {
        int width = source.getWidth();
        int height = source.getHeight();
        float scaleWidth = ((float) toWidth) / width;
        float scaleHeight = ((float) toHeight) / height;
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        return Bitmap.createBitmap(source, 0, 0, toWidth, toHeight, matrix, false);
    }
Fragment
  • 1,555
  • 1
  • 26
  • 33
  • this one remove the nullpointer but cut de bitmap.I modify it and work but i think im doing the same as createEScaledBitmap.This is mi new code private Bitmap changeDimens(bitmap,int width,int height) { int bwidth = bitmap.getWidth(); int bheight = bitmap.getHeight(); float scaleWidth = ((float) width) / bwidth; float scaleHeight = ((float) height) / bheight; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); return Bitmap.createBitmap(bitmap, 0, 0, bwidth, bheight, matrix, false); } – Marcos Octavio Guerra Liso Jan 10 '19 at 14:45
0

One thing looks off

Matrix matrix = new Matrix(); 
matrix.postScale(width, height);

But your width and height are integers. My guess is they are the actual bitmap pixel size you want. These are floating point values and they are relative to the current size. So if you wanted to make the bitmap 1/2 as big you might use

matrix.postScale(0.5, 0.5);

And if you wanted to make it 2 times as big

matrix.postScale(2, 2);

Now lets assume that your width and height are some size value like 200. Then this line would be creating a bitmap that was 200 times wider and 200 times taller and create bitmap would fail on out of memory for sure. If you pass in less than 1 for both the integer value would be 0 and you would get a zero size bitmap.

Marc
  • 1,159
  • 17
  • 31