1

I have an int[] intArray, i can make a bitmap like :

Bitmap bitmap = Bitmap.createBitmap(intArray, width, height, Bitmap.Config.ARGB_8888);

Now, i want to use byte[] to make this bitmap, so i try convert int[] to byte[], but it does't work, the bitmap it make seems lack some color

public static byte[] intArrayToByteArray(int[] intArray) { byte[] ba = new byte[intArray.length * 4];

    for(int i = 0, k = 0; i < intArray.length; i++) {
        int temp= intArray[i];
        for(int j = 0; j < 4; j++, k++) {
            ba[k] = (byte)((temp>> (8 * j)) & 0xFF);
        }
    }
    return ba;

}

public static Bitmap byteArrayToBitmap(byte[] byteArray, int width, int height) {
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.copyPixelsFromBuffer(buffer);
    return bitmap;
}

the bitmap it make seems lack some color by this way.

But if I make bitmap by int[] firstly, and then converted bitmap to byte[], and the byte[] seems to be correct!! This way is not good.

So does somebody has a way convert int[] to byte[], so that we can use byte[] to make a bitmap indtead of using int[].

1 Answer 1

0

You need set alpha = false -> bitmap.setHasAlpha(false)

public static Bitmap byteArrayToBitmap(byte[] byteArray, int width, int height) {
    ByteBuffer buffer = ByteBuffer.wrap(byteArray);
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bitmap.setHasAlpha(false);
    bitmap.copyPixelsFromBuffer(buffer);
    return bitmap;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.