Camille
Camille

Reputation: 193

How to get the local path from a file downloaded from Firebase Storage

I am a bit confused with the Firebase documentation: https://firebase.google.com/docs/storage/android/download-files

I am trying to download a file from Firebase through the URL of the file and then get its local path:

mStorageReference.getFile(downloadURL).addOnSuccessListener(new 
    OnSuccessListener<FileDownloadTask.TaskSnapshot>() {

    @Override
    public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {

        // I assume the file is now downloaded from the given URL and is on the device
        // HOW DO I GET THE PATH TO THE FILE ON THE DEVICE ?
    }
});

The question is in the comments.

Upvotes: 0

Views: 1530

Answers (1)

Rohan Stark
Rohan Stark

Reputation: 2396

From the docs :-

The getFile() method downloads a file directly to a local device.

So, instead of doing what you're doing, you can first create a temporary file. Following is an example :-

File localFile = File.createTempFile("images", "jpg");

After that, you pass this localFile as a parameter to your getFile() method ( instead of passing downloadURL ). So, when your onSuccess() is fired, this file is populated with the data that has been downloaded and you can access it for whatever you need. Something like this :-

mStorageReference.getFile(localFile).addOnSuccessListener(new 
OnSuccessListener<FileDownloadTask.TaskSnapshot>() {

@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
      //localFile contains your downloaded data

}
});

Note that in this example, the localFile is temporary, but you can create a File at your specified path too. It depends on your use case.

Upvotes: 3

Related Questions