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

时间:2018-04-05 06:57:37

标签: android image bitmap 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文件应用程序崩溃,它可以正常工作。 出现此错误

致命的例外:主要                                                                           处理:app.com.imageuploadexample,PID:6984                                                                           java.lang.IllegalArgumentException:width和height必须是> 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)                                                                               at java.lang.reflect.Method.invoke(Native Method)                                                                               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:937)                                                                               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:798)

1 个答案:

答案 0 :(得分:1)

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

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

}