How do you transform a Bitmap
into an InputStream
?
I would like to use this InputStream
as input to the ETC1Util.loadTexture()
function.
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);
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With