1

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;

2 Answers 2

2

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 ! :)

Sign up to request clarification or add additional context in comments.

6 Comments

So just; byte[] bytes = BitmapFactory.decodeStream(stream);?
Bitmap bitmap = BitmapFactory.decodeStream(stream)
+1 For the most right answer. For transparent images (such as: .png), I think you need to specify BitmapFactory.Options.
Seems to work but if i want to put the ImageView in to my LinearLayout it says: Only the original thread that created a view hierarchy can touch its views How to fix this?
@LittleChild You should include the main looper: new Handel(Looper.getMainLooper()).post(runnable);
|
2

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);

4 Comments

BitmapFactory.decodeStream() because so that I can make a Bitmap of it :p
Still very complex :D Just pass in the stream to decodeStream() and let Android do the magic ;) It is overloaded. :)
@LittleChild Yeah agreed! Too lazy to update it twice :P
I tried this now but then it says NullPointerException for outputstream write at: while((theByte = is.read()) != -1) baos.write(bytes); i think "is" should mean stream right?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.