Android: Transform a bitmap into an input stream

后端 未结 2 1833
陌清茗
陌清茗 2020-12-24 05:14

How do you transform a Bitmap into an InputStream?

I would like to use this InputStream as input to the ETC1Util.loadTex

2条回答
  •  不知归路
    2020-12-24 06:05

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

提交回复
热议问题