I've been trying to get the metadata (md5hash) of an image file from Firebase Storage and check if it's not equal with the md5hash of an image file on the user's phone. The problem is that even though the hashes are the same I get the result that they are different.
This is the code I am trying to get the metadata and compare it:
for(int i = 0; i<5; i++) {
StorageReference forestRef = storageRef.child("profile_images/img_" + (i + 1) + ".jpg");
final int finalI = i;
forestRef.getMetadata().addOnSuccessListener(new OnSuccessListener<StorageMetadata>() {
@Override
public void onSuccess(StorageMetadata storageMetadata) {
if (!getMD5(getFilesDir() + "/Images/img_" + finalI + ".jpg").equals(storageMetadata.getMd5Hash())) {
System.out.println("not equal");
//if image files differ then download them
System.out.println(storageMetadata.getMd5Hash());
System.out.println(getMD5(getFilesDir() + "/Images/img_" + finalI + ".jpg"));
StorageReference islandRef = storageRef.child("profile_images/img_" + (finalI + 1) + ".jpg");
final long ONE_MEGABYTE = 1024 * 1024;
islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
@Override
public void onSuccess(byte[] bytes) {
//download files here
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
}
});
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Uh-oh, an error occurred!
}
});
}
This is the getMD5 method:
private String getMD5(String filePath)
{
String base64Digest = "";
try
{
InputStream input = new FileInputStream(filePath);
byte[] buffer = new byte[1024];
MessageDigest md5Hash = MessageDigest.getInstance("MD5");
int numRead = 0;
while (numRead != -1)
{
numRead = input.read(buffer);
if (numRead > 0)
{
md5Hash.update(buffer, 0, numRead);
}
}
input.close();
byte [] md5Bytes = md5Hash.digest();
base64Digest = Base64.encodeToString(md5Bytes, Base64.DEFAULT);
/*for (byte md5Byte : md5Bytes) {
returnVal += Integer.toString((md5Byte & 0xff) + 0x100, 16).substring(1);
}*/
}
catch(Throwable t) {t.printStackTrace();}
return base64Digest;
I am getting this output:
I/System.out: not equal
I/System.out: zy1sZIW0XO6kH01g9LgFfw==
I/System.out: zy1sZIW0XO6kH01g9LgFfw==
I/System.out: not equal
I/System.out: wi2/XGYDD4ncHaNSRKct+A==
I/System.out: wi2/XGYDD4ncHaNSRKct+A==
I/System.out: not equal
I/System.out: DBmKPXhzYQcqGb/twjihEg==
I/System.out: DBmKPXhzYQcqGb/twjihEg==
I/System.out: not equal
I/System.out: beq6gp3s8cQ9Ky9Gn7/KoA==
I/System.out: beq6gp3s8cQ9Ky9Gn7/KoA==
I/System.out: not equal
I/System.out: tcuOskaSmP5HcaqCAszAuA==
I/System.out: tcuOskaSmP5HcaqCAszAuA==
Try to use this condition in the if:
if (getMD5(getFilesDir() + "/Images/img_" + finalI + ".jpg").trim().compareTo(storageMetadata.getMd5Hash().trim()) != 0) {
//[...]
}
instead of ! equals
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With