Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Transform a bitmap into an input stream

Tags:

android

How do you transform a Bitmap into an InputStream?

I would like to use this InputStream as input to the ETC1Util.loadTexture() function.

like image 545
tjb Avatar asked Oct 08 '11 17:10

tjb


2 Answers

This might work

ByteArrayOutputStream bos = new ByteArrayOutputStream();  bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);  byte[] bitmapdata = bos.toByteArray(); ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata); 
like image 151
blessanm86 Avatar answered Sep 19 '22 23:09

blessanm86


This is my way:

// Your Bitmap. Bitmap bitmap = XXX;    int byteSize = bitmap.getRowBytes() * bitmap.getHeight(); ByteBuffer byteBuffer = ByteBuffer.allocate(byteSize); bitmap.copyPixelsToBuffer(byteBuffer);    // Get the byteArray. byte[] byteArray = byteBuffer.array();  // Get the ByteArrayInputStream. ByteArrayInputStream bs = new ByteArrayInputStream(byteArray); 
like image 23
jasonnn Avatar answered Sep 20 '22 23:09

jasonnn