gpt4 book ai didi

android - 图像的字节数组到 imageview

转载 作者:IT老高 更新时间:2023-10-28 21:36:35 26 4
gpt4 key购买 nike

我搜索了一下,但无法清楚地看到它。如何将图像的字节数组设置为 ImageView?我试过了,但没有用。

BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));

最佳答案

这是您如何将 Bitmap 转换为 ByteArray 并将 ByteArray 转换为 Bitmap 的方法:


将位图转换为字节数组:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

将字节数组转换为位图:

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false));

关于android - 图像的字节数组到 imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13854742/

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