How to check if a file exists in Firebase storage from an Android application?

I am developing an Android application where the user clicks an image, it is stored in firebase, cloud functions process this image and save the output back to firebase as a text file. To display the output in android, the application continues to check the output file if it exists or not. If so, then it displays the output in the application. If not, I must continue to wait until the file is available.

I cannot find documentation to check if any file exists in Firebase or not. Any help or pointers would be helpful.

Thank.

+4
source share
3

API Firebase , , .

, :

+1

getDownloadURL, Promise, , , " " , . :

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

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

.

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

 void getReferenceAndLoadNewBackground(String photoName) {
    final StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("Photos").child(photoName + ".png");
    storageReference.getDownloadUrl()
            .addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    loadBackground(storageReference);
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    int errorCode = ((StorageException) exception).getErrorCode();
                    if (errorCode == StorageException.ERROR_OBJECT_NOT_FOUND) {
                        StorageReference storageReference2 = FirebaseStorage.getInstance().getReference().child("Photos").child("photo_1.png");
                        loadBackground(storageReference2);
                    }

                }
            });
}
0

Source: https://habr.com/ru/post/1675430/


All Articles