【发布时间】:2019-04-16 10:57:51
【问题描述】:
我有一个应用程序允许用户将他们的个人资料图像更改为存储在他们设备上的另一个图像。为此,应用程序将选定的图像发送到远程服务器。这一切都很好,所以我不放那部分的代码,以免使问题复杂化。我的问题是我希望减小发送到服务器的位图的大小,以防止例如 5 或 6 兆字节的大文件,这会降低应用程序的速度。但结局并不好。
这是我的代码:
if (requestCode == 1 && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
imagepath = getPath(selectedImageUri);
Bitmap bitmap=BitmapFactory.decodeFile(imagepath);
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
//Resize the bitmap
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 150, 150, false);
resizedBitmap.compress(Bitmap.CompressFormat.JPEG,50,bytearrayoutputstream);
//Round the image
int min = Math.min(resizedBitmap.getWidth(), resizedBitmap.getHeight());
Bitmap bitmapRounded = Bitmap.createBitmap(min, min, resizedBitmap.getConfig());
Canvas canvas = new Canvas(bitmapRounded);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(new BitmapShader(resizedBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
canvas.drawRoundRect((new RectF(0.0f, 0.0f, min, min)), min / 2, min / 2, paint);
//bitmap to imageView
avatar.setImageBitmap(bitmapRounded);
}
我试图缩小它的大小(它不起作用),然后在将其调整到相应的 ImageView 之前对图像进行四舍五入(这很好)。
唯一不能让它正常工作的就是减小位图的大小。
【问题讨论】: