Firebase存储getMetadata()问题 [英] Firebase Storage getMetadata() issue

查看:103
本文介绍了Firebase存储getMetadata()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我试图获取元数据和比较它的代码: (int i = 0; i <5; i ++){
StorageReference forestRef = storageRef.child(i $ 5)

  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);
/ /如果图像文件不同,然后下载
System.out.println(storageMetadata.getMd5Hash());
System.out.println(getMD5(getFilesDir()+/ Images / img_+ finalI +.jpg));
StorageReference islandRef = storageRef.child(profile_images / img_+(finalI + 1)+.jpg);
最终长ONE_MEGABYTE = 1024 * 1024;
islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener< byte []>(){
@Override
public void onSuccess(byte [] bytes){
)在这里下载文件

)addOnFailureListener(new OnFailureListener(){
@Override
public void onFailure(@NonNull异常异常){
/ /处理任何错误
}
});

$ b})。addOnFailureListener(new OnFailureListener(){
@Override
public void onFailure(@NonNull异常异常){
//呃,发生错误!
}
});



$ b

这是getMD5方法:

  private String getMD5(String filePath)
{
String base64Digest =;
尝试
{
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); (byte md5Byte:md5Bytes){
returnVal + = Integer.toString((md5Byte& 0xff)+ 0x100,16).substring(1);
$ b / *


catch(Throwable t){t.printStackTrace();}
返回base64Digest;

我得到这个输出:

  I / System.out:不等于
I / System.out:zy1sZIW0XO6kH01g9LgFfw ==
I / System.out:zy1sZIW0XO6kH01g9LgFfw ==
I / System .out:不等于
I / System.out:wi2 / XGYDD4ncHaNSRKct + A ==
I / System.out:wi2 / XGYDD4ncHaNSRKct + A ==
I / System.out:not等于
I / System.out:DBmKPXhzYQcqGb / twjihEg ==
I / System.out:DBmKPXhzYQcqGb / twjihEg ==
I / System.out:不等于
I / System .out:beq6gp3s8cQ9Ky9Gn7 / KoA ==
I / System.out:beq6gp3s8cQ9Ky9Gn7 / KoA ==
I / System.out:不等于
I / System.out:tcuOskaSmP5HcaqCAszAuA ==
I / System.out:tcuOskaSmP5HcaqCAszAuA ==


解决方案

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

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

而不是!等于


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

这篇关于Firebase存储getMetadata()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆