2012-12-13 35 views
22

我已经查了一下,但不能清楚地看到它。我怎样才能将一个图像的字节数组设置为一个图像视图?我得到了字符串在java中尝试BufferedImage img = ImageIO.read(new ByteArrayInputStream(bytes));,但不能:(任何人都可以帮助我?对不起,如果问题就像一个小白菜:)图像字节数组到imageview

回答

52

尝试下面的代码转换位图为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(); 

转换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))); 
+0

感谢名单的队友,将检查和电话你 –

+0

权在钱:) thanx很多 –

+3

可能有任何OutOfMemoryError与此? –

-1

获得字节从位图

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream); 
    byte[] bytearray = stream.toByteArray(); 
    return bytearray; 
+0

这是你给的错误答案。 –