gpt4 book ai didi

android - Firebase 存储 getMetadata() 问题

转载 作者:搜寻专家 更新时间:2023-11-01 09:38:31 25 4
gpt4 key购买 nike

我一直在尝试从 Firebase 存储中获取图像文件的元数据 (md5hash),并检查它是否与用户手机上图像文件的 md5hash 不相等。问题是即使哈希值相同,我得到的结果也是不同的。

这是我尝试获取元数据并进行比较的代码:

    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!
}
});
}

这是 getMD5 方法:

    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/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==

最佳答案

尝试在 if 中使用这个条件:

 if (getMD5(getFilesDir() + "/Images/img_" + finalI + ".jpg").trim().compareTo(storageMetadata.getMd5Hash().trim()) != 0) {
//[...]
}

而不是!等于

关于android - Firebase 存储 getMetadata() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41380078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com