Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a md5 sum from an Android Bitmap object

I've spent several hours trying to figure out how to do this. I've read post after post here on stackoverflow and the documentation.

I have a android.graphics.Bitmap object and I need to get it's md5 sum. At the point that I want to verify the sum it has not been saved to the file system. I've seen several ways of doing this for java.io.File objects. I just need a function that receives a Bitmap object and returns the hex md5 sum as a String.

This might have been addressed somewhere but if it has been I have been unable to understand it or deduce how to do it from it.

The less resource heavy the method is the better it is of course.

like image 324
Bjorninn Avatar asked Jan 14 '23 01:01

Bjorninn


1 Answers

Get bitmap's bytes to calculate md5.

Bitmap bm = ... // your bitmap
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object   
byte[] bitmapBytes = baos.toByteArray();

So you have byte array now. You can find how to get md5 hash of byte array in android here.

like image 134
Leonidos Avatar answered Jan 19 '23 11:01

Leonidos