将图像的字节数组转换为imageview

Man*_*mar 36 android android-imageview

我搜索了一下但是无法清楚地看到它.如何将Image的字节数组设置为imageview?我BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));在java中尝试了字符串,但不能:(任何人都可以帮助我吗?抱歉,如果问题就像一个菜鸟:)

Dip*_*iya 95

尝试下面的代码将位图转换为bytearray和bytearray转换为位图,它将解决您的问题.

将位图转换为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();
Run Code Online (Sandbox Code Playgroud)

将ByteArray转换为位图: -

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));
Run Code Online (Sandbox Code Playgroud)

  • 可能有任何OutOfMemoryError吗? (3认同)