如何从Android应用程序检查Firebase存储中是否存在文件?

问题描述:

我正在开发一个Android应用程序,用户单击图像,将其存储在Firebase中,云功能处理该图像并将输出以文本文件的形式存储在Firebase中.为了在android中显示输出,应用程序会不断检查输出文件是否存在.如果是,那么它将在应用程序中显示输出.如果没有,我必须一直等待文件,直到文件可用为止.

I am developing android application where a user clicks image, it gets stored in firebase, cloud functions process this image and stores the output back in the firebase in the form of text file. In order to display the output in android, application keeps checking for output file if it exists or not. If yes, then it displays the output in the application. If no, I have to keep waiting for the file till it is available.

我找不到任何文档来检查Firebase中是否存在任何文件.任何帮助或指示将是有帮助的.

I'm unable to find any documentation for checking if any file is exists in Firebase or not. Any help or pointers will be helpful.

谢谢.

您可以使用getDownloadURL返回一个Promise,该Promise可以用来捕获未找到"错误,或者处理该文件(如果存在).例如:

You can use getDownloadURL which returns a Promise, which can in turn be used to catch a "not found" error, or process the file if it exists. For example:

    storageRef.child("file.png").getDownloadURL().then(onResolve, onReject);

function onResolve(foundURL) { 
//stuff 
} 
function onReject(error){ 
//fill not found
console.log(error.code); 
}

已更新

这是另一个更简单,更干净的解决方案.

This is another simpler and cleaner solution.

storageRef.child("users/me/file.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        // Got the download URL for 'users/me/profile.png'
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // File not found
    }
});