首页 文章

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

提问于
浏览
-1
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文件应用程序崩溃,它可以正常工作 . 得到了这个错误

致命异常:主要进程:app.com.imageuploadexample,PID:6984 java.lang.IllegalArgumentException:android.graphics.Bitmap上android.graphics.Bitmap.createBitmap(Bitmap.java:841)的宽度和高度必须> 0 . 位于app.com.imageuploadexample.MainActivity.getResizedBitmap(MainActivity.java:108)的android.graphics.Bitmap.createBitmap(Bitmap.java:751)的createBitmap(Bitmap.java:820)位于app.com.imageuploadexample.MainActivity $ 1 . 运行(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)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 回答

  • 1

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

    公共类BitmapConvert扩展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;
    }
    

    }

相关问题