How to check if a file exists in Firebase storage from your android application?

前端 未结 5 1798
忘掉有多难
忘掉有多难 2021-01-01 19:38

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 t

5条回答
  •  暖寄归人
    2021-01-01 20:33

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

    Updated

    This is another simpler and cleaner solution.

    storageRef.child("users/me/file.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener() {
        @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
        }
    });
    

提交回复
热议问题