Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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.

Thanks.

like image 950
Sarvesh Kulkarni Avatar asked Apr 23 '17 05:04

Sarvesh Kulkarni


People also ask

How do I find the path of a file in Firebase storage?

If you want the file path on firebase storage to access it via storage reference and not URL, you can use taskSnapshot. getStorage().

How do I search in Firebase storage?

There is no way to list files, search for files or filter files within the Firebase Storage API. There are some such features in the gcloud API, which can also be used on Firebase Storage. This user is first on the weekly Google Cloud leaderboard.

How do I access Firebase storage on Android?

From the navigation pane of the Firebase console, select Storage, then click Get started. Review the messaging about securing your Cloud Storage data using security rules. During development, consider setting up your rules for public access. Select a location for your default Cloud Storage bucket.


1 Answers

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<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
    }
});
like image 171
Christlin Panneer Avatar answered Sep 19 '22 12:09

Christlin Panneer