我希望我的应用程序上传没有大小限制的图像,但是在代码中,如果图像大小超出限制,我想将图像大小调整为1MB.我已经尝试了许多方法,但是找不到上面提到的要求的任何代码.

我已经尝试过一次:

public void scaleDown() {

int width = stdImageBmp.getWidth();

int height = stdImageBmp.getHeight();

Matrix matrix = new Matrix();

float scaleWidth = ((float) MAX_WIDTH) / width;

float scaleHeight = ((float) MAX_HEIGHT) / height;

matrix.postScale(scaleWidth, scaleHeight);

stdImageBmp = Bitmap.createBitmap(stdImageBmp, 0, 0, width, height, matrix, true);

File Image = new File("path");

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

//compress bmp

stdImageBmp.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);

byte[] byteArray = byteArrayOutputStream.toByteArray();

imgViewStd.setImageBitmap(stdImageBmp);

Log.d("resizedBitmap", stdImageBmp.toString());

width = stdImageBmp.getWidth();

height = stdImageBmp.getHeight();

System.out.println("imgWidth" + width);

System.out.println("imgHeight" + height);

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐