App crashes on converting a bitmap of png image

2019-09-20 00:29发布

public Bitmap getResizedBitmap(Bitmap bm, int newWidth) {
    int newHeight;
    int width = bm.getWidth();
    int height = bm.getHeight();
    double aspect_ratio = width/height;
    newHeight = (int) (newWidth*aspect_ratio);
    if(iv!=null){
        ViewGroup.LayoutParams params = iv.getLayoutParams();
        params.height = newHeight;
        iv.setLayoutParams(params);
    }
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
            matrix, false);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    Log.e(TAG, "getResizedBitmap: bse64" + getBase64(resizedBitmap) );
    return resizedBitmap;
}

Here I am converting a bitmap to downscale it. But on png bitmap app get crashes. It works correctly if I choose jpg files but on selecting png file app crashes. Got this error

FATAL EXCEPTION: main Process: app.com.imageuploadexample, PID: 6984 java.lang.IllegalArgumentException: width and height must be > 0 at android.graphics.Bitmap.createBitmap(Bitmap.java:841) at android.graphics.Bitmap.createBitmap(Bitmap.java:820) at android.graphics.Bitmap.createBitmap(Bitmap.java:751) at app.com.imageuploadexample.MainActivity.getResizedBitmap(MainActivity.java:108) at app.com.imageuploadexample.MainActivity$1.run(MainActivity.java:75) at android.os.Handler.handleCallback(Handler.java:815) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:238) at android.app.ActivityThread.main(ActivityThread.java:6016) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)

1条回答
Animai°情兽
2楼-- · 2019-09-20 00:58

you can try below code to solve your problem

public class BitmapConvert extends AppCompatActivity { Bitmap bmp;

ImageView imageview_convert;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_bitmap_convert);
    imageview_convert= (ImageView) findViewById(R.id.imageview_convert);


    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.nature);


    imageview_convert.setImageBitmap(getResizedBitmap(bmp,300,200));
}

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
    int width = bm.getWidth();
    int height = bm.getHeight();
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    Bitmap resizedBitmap = Bitmap.createBitmap(
            bm, 0, 0, width, height, matrix, false);

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

    return resizedBitmap;
}

}

查看更多
登录 后发表回答