Firebase存储getMetadata()问题

时间:2016-12-29 12:49:34

标签: android firebase firebase-storage

我一直在尝试从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==

1 个答案:

答案 0 :(得分:1)

尝试在if:

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

而不是! equals