【问题标题】:Convert Bitmap to Byte array WITHOUT Bitmap.CompressFormat [duplicate]将位图转换为字节数组而不使用 Bitmap.CompressFormat [重复]
【发布时间】:2013-06-10 11:55:30
【问题描述】:

有没有办法在 Android 中不使用 Bitmap.CompressFormat 类将位图转换为字节数组?如果没有,怎么会?如果可能的话,请告诉我怎么做,谢谢

【问题讨论】:

    标签: android bitmap bytearray


    【解决方案1】:

    这样的?

    //b is the Bitmap
    
    //calculate how many bytes our image consists of.
    int bytes = b.getByteCount();
    //or we can calculate bytes this way. Use a different value than 4 if you don't use 32bit images.
    //int bytes = b.getWidth()*b.getHeight()*4; 
    
    ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
    b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
    
    byte[] array = buffer.array(); //Get the underlying array containing the data.
    

    从; https://stackoverflow.com/a/10192092/1868384

    【讨论】:

    • 是否可以在不使用 Bitmap.CompressFormat 的情况下将此数组保存为 jpg? @斯蒂芬
    猜你喜欢
    • 2019-05-13
    • 2010-09-26
    • 2018-05-06
    • 1970-01-01
    • 2015-11-23
    • 2013-01-14
    • 2013-07-29
    相关资源
    最近更新 更多