应用程序在转换png图像的位图时崩溃 [英] App crashes on converting a bitmap of png image

查看:152
本文介绍了应用程序在转换png图像的位图时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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位图应用上出现崩溃.如果我选择jpg文件,但选择png文件时应用程序崩溃,它可以正常工作. 出现此错误

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

致命异常:主要 流程: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)

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

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

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

}

这篇关于应用程序在转换png图像的位图时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆