Question

Has anyone an idea how to get a BufferedInputStream to a byte[] so that I can make a Bitmap of it?

That's my Stream:

BufferedInputStream stream = new BufferedInputStream(socket.getInputStream());

And that's my byte[]:

byte[] bytes;
Was it helpful?

Solution

uhm... the read() method of the BufferedInputStream will give you the byte[] you need.

For getting a Bitmap, use the BitmapFactory.decodeStream() method and pass in your BufferedInputStream as a parameter. Good to go ! :)

OTHER TIPS

Use ByteArrayOutputStream:

BufferedInputStream stream = new BufferedInputStream(s.getInputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();

int theByte;

while((theByte = stream.read()) != -1) baos.write(theByte);

Then:

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
byte[] imageByteArray = bytes;
Bitmap bitmap = BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length, opt);
imageView.setImageBitmap(bitmap);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top