Android 应用程序在转换png图像的位图时崩溃

Android 应用程序在转换png图像的位图时崩溃,android,image,bitmap,png,Android,Image,Bitmap,Png,在这里,我转换一个位图来缩小它。但在png位图应用程序上会崩溃。如果我选择jpg文件,它可以正常工作,但选择png文件时,应用程序崩溃。 得到了这个错误 致命异常:主 进程:app.com.imageuploadexample,PID:6984 java.lang.IllegalArgumentException:宽度和高度必须大于0 位于android.graphics.Bitmap.createBitmap(Bitmap.java:841) 位于android.graphics.Bitmap

在这里,我转换一个位图来缩小它。但在png位图应用程序上会崩溃。如果我选择jpg文件,它可以正常工作,但选择png文件时,应用程序崩溃。 得到了这个错误

致命异常:主 进程:app.com.imageuploadexample,PID:6984 java.lang.IllegalArgumentException:宽度和高度必须大于0 位于android.graphics.Bitmap.createBitmap(Bitmap.java:841) 位于android.graphics.Bitmap.createBitmap(Bitmap.java:820) 位于android.graphics.Bitmap.createBitmap(Bitmap.java:751) 在app.com.imageuploadexample.MainActivity.getResizedBitmap(MainActivity.java:108)上 在app.com.imageuploadexample.MainActivity$1.run(MainActivity.java:75) 位于android.os.Handler.handleCallback(Handler.java:815) 位于android.os.Handler.dispatchMessage(Handler.java:104) 位于android.os.Looper.loop(Looper.java:238) 位于android.app.ActivityThread.main(ActivityThread.java:6016) 位于java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:937)上
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)

您可以尝试以下代码来解决您的问题

公共类BitmapConvert扩展了AppCompatActivity{ 位图bmp

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;
}

}

应用程序在转换png图像位图时崩溃,崩溃日志在哪里可能重复要更正的代码中存在错误:“newHeight=(int)(newWidth/aspect_ratio);”。它应该是一个除法。或者,您可以传递静态宽度和高度,而不是bm.getWidth()&bm.getHeight(),以进行测试。我刚刚通过更改此行double aspect\u ratio=width/height;双倍纵横比=(双倍)宽度/高度;谢谢大家!
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;
}