图像的字节数组的ImageView数组、字节、图像、ImageView

2023-09-06 01:02:45 作者:起床困难户

我已经搜查了一点,但不能得到它的一个明显的一瞥。如何设置图像的字节数组到一个ImageView的?我得到的字符串试图 BufferedImage的IMG = ImageIO.read(新ByteArrayInputStream的(字节)); 在Java中,但不能:(谁能帮助我对不起,如果这个问题?就像一个小白:)

I've searched a bit but cant get a clear glimpse of it. How can I set a byte array of an Image into an imageview? I got the string tried BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes)); as in java but not able to :( can anyone help me? sorry if the question was like a noob :)

推荐答案

尝试下面code为转换位图的字节数组,并B​​yteArray的位图,它会解决你的问题。

Try below code for convert bitmap to bytearray and bytearray to bitmap, it will solve your problem.

位图转换为ByteArray中: -

Convert Bitmap to ByteArray:-

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

转换ByteArray的位图: -

Convert ByteArray to Bitmap:-

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);