问题
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.
回答1:
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.
回答2:
I'm not Android developer, but I see in the API reference (http://developer.android.com/reference/android/graphics/Bitmap.html) that there are methods for:
- getting size of the bitmap: getWidth, getHeight
- getting the pixels as an array of integers: getPixels
So you could just create an array of needed size, then read all the pixels, and convert the array to byte[].
Then it should be not a problem to calculate md5 sum from it.
来源:https://stackoverflow.com/questions/15158651/generate-a-md5-sum-from-an-android-bitmap-object