I have tried several different methods found in the java documentation, as well as several solutions from other SO questions and have successfully gotten a Bitmap
to convert to a byte[]
and back again.
The problem is that I now need to convert this byte[] to a String
, then back to a byte[]
, then back to a Bitmap
again. To recap what I need:
Bitmap -> byte[] -> String -> byte[] -> Bitmap
I know this sounds strange but what I'm trying to accomplish must be done this way. Below is what I have tried, if anyone could point out what I'm doing wrong I'd greatly appreciate it!
Bitmap bitmap = mv.getDrawingCache();
// Convert bitmap to byte[]
ByteArrayOutputStream output = new ByteArrayOutputStream(bitmap.getByteCount());
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
byte[] imageBytes = output.toByteArray();
// Convert byte[] to string
// I have also tried using Base64.encodeToString(imageBytes, 0);
String encodedString = new String(imageBytes);
// Convert string to byte[]
byte[] newImageBytes = encodedString.getBytes();
if (imageBytes == newImageBytes) {
Toast.makeText(SignatureActivity.this, "SUCCESS!!", Toast.LENGTH_SHORT).show();
} else { // Sadly, we always get to this point :(
Toast.makeText(SignatureActivity.this, "BOOO", Toast.LENGTH_SHORT).show();
}
// Convert byte[] back to bitmap
bitmap = BitmapFactory.decodeByteArray(newImageBytes, 0, newImageBytes.length);
Again, going Bitmap -> byte[] -> Bitmap
was successful, but adding in the conversion to a String
and back is causing the final Bitmap
to write an image of 0kb.