0

I need to download bitmap, encode it and save to sd card. I have no problems with 1st and 3rd steps, but with 2nd. I don't need sophisticated encoding algorithm, so I just reverse byte array which is received during download:

private static byte[] vice_versa(byte[] bytes) {
    if (bytes == null) {
        assert false;
        return null;
    }
    byte[] result = new byte[bytes.length];
    for (int i = 0; i < bytes.length; i++) {
        result[bytes.length - 1 - i] = bytes[i];
    }
    return result;
}

and save it to sd card:

public boolean saveToFile(byte[] bytes) {
        ...
        bytes = vice_versa(bytes);
        out = new FileOutputStream(getFile());
        out.write(bytes);
        ...

I load saved file like this:

            ...
            byte[] bytes = FileUtils.readFileToByteArray(getFile());
            bytes = vice_versa(bytes);              
            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, 0);
            ...

However, BitmapFactory cannot decode byte array and returns null. I am able to compare decoded byte stream, with original byte stream. Stream lengths are same, all bytes are same, but Object.equals returns false. How is it possible and what is going on?

Eugene Chumak
  • 3,272
  • 7
  • 34
  • 52

1 Answers1

0

Your question is not very clear (which equals() are you talking about???), but it seems that you confuse array1.equals(array2) and Arrays.equals(array1, array2), see equals vs Arrays.equals in Java

Community
  • 1
  • 1
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
  • I really mixed up array1.equals() and Arrays.equals(). In fact, Arrays.equals() returns true, what I did wrong is passing 3rd argument as 0 to bmp factory: BitmapFactory.decodeByteArray(bytes, 0, 0); instead should be: BitmapFactory.decodeByteArray(bytes, 0, bytes.length); – Eugene Chumak Apr 09 '14 at 09:32