gpt4 book ai didi

Android,如何减小位图的大小?

转载 作者:行者123 更新时间:2023-11-29 00:54:36 25 4
gpt4 key购买 nike

我有一个应用程序,允许用户将他们的个人资料图像更改为存储在他们设备上的另一个图像。为此,应用程序将选定的图像发送到远程服务器。所有这一切都很好,所以我没有放那部分的代码以免使问题复杂化。我的问题是我希望减小发送到服务器的位图的大小,以防止出现例如五六兆字节的大文件,这会降低应用程序的速度。但结局并不好。

这是我的代码:

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 之前将图像四舍五入(这很好用)。

我唯一无法让它正常工作的是减小位图的大小。

最佳答案

我正在使用它来减小我的位图大小并且它工作正常:-

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos);

关于Android,如何减小位图的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55706582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com