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)
you can try below code to solve your problem
public class BitmapConvert extends AppCompatActivity { Bitmap bmp;
}