I made a Bitmap out of a byte array. I want to save the bitmap to a file. I then want to retrieve that Bitmap and restore the original byte array. I thought I could just simply save the file and then read it, but apparently the bytes are different...
Saving bitmap:
// create the bitmap
final Bitmap bitmap = Bitmap.createBitmap(width, newHeight, Bitmap.Config.ARGB_8888);
ByteBuffer buffer = ByteBuffer.wrap(originalBytes);
bitmap.copyPixelsFromBuffer(buffer);
...
// save the bitmap to file
String dir_path = Environment.getExternalStorageDirectory().getAbsolutePath();
File dir = new File(dir_path);
if(!dir.exists())
{
dir.mkdirs();
}
File file = new File(dir, "meh.png");
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I then read this file
// read the file
String file_path = Environment.getExternalStorageDirectory().getAbsolutePath();
Bitmap bitmap = BitmapFactory.decodeFile(file_path + "/" + "meh.png");
// get the bytes
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
bitmap.copyPixelsToBuffer(buffer);
byte[] retrievedBytes = buffer.array();
When I compare originalBytes with retrievedBytes, the lengths are the same, but the content is not. I thought Bitmap.CompressFormat.PNG guarantees no compression? What am I missing here?
EDIT:
Here are parts of the both byte arrays converted to hex
original bytes hex: 8D7D A111 DE1D 105F 0B58 86A0 5F4D 035A D9A6 ...
retrieved bytes hex: 0406 0711 1F1D 105F 0B58 86A0 054D 035A 3C09 ...