getDownloadUrl Firebase Storage returns Null (Android)

I am developing a little project and i have some issues about firebase Storage and getDownloadUrl. I have some images already uploaded on FirebaseStorage but when I try to get the download Url it return nulls.

Here is the code: Imports:

import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

Function getImage()

public void getImage(){
    StorageReference myStorage = FirebaseStorage.getInstance().getReference();
    StorageReference newStorage = myStorage.child("picture").child("pic_one.jpg");
    newStorage.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            myuri = uri;
        }
    });
}

The rules on Firebase Storage without any authentication

service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
  allow read, write;   
    }
  }
}

When the app runs the line getDownloadUrl doesn't do anything, I mean I want to retrieve the https link to show the picture in another activity using glide, but I just get null on myuri variable. The variable myuri is defined as URI.

Thanks in advance.

Upvotes: 0

Views: 2927

Answers (1)

Gast&#243;n Saill&#233;n
Gast&#243;n Saill&#233;n

Reputation: 13129

Try to do this:

private String generatedFilePath;

 myStorage.child("picture").child("pic_one.jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            // Got the download URL for 'pic_one.jpg'
            Uri downloadUri = taskSnapshot.getMetadata().getDownloadUrl();
            generatedFilePath = downloadUri.toString(); /// The string(file link) that you need
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });

Also, it is not possible to get the downloadURL from the root of the storage tree. You should store the downloadURL of the file programmatically to your database in order to access it later on, so first upload the photo to your storage and then in the onSuccess you should upload the downloadURL to your database, then retrieve it from there.

In order to do this you should first declare your databaseReference

private DatabaseReference mDatabase;
// ...
mDatabase = FirebaseDatabase.getInstance().getReference();

and then, after you upload succefully your picture to the storage, grab the downloadURL and post it to your database

this is an example from the official doc

    Uri file = Uri.fromFile(new File("path/to/images/rivers.jpg"));
    StorageReference riversRef = storageRef.child("images/"+file.getLastPathSegment());
    uploadTask = riversRef.putFile(file);
    
    // Register observers to listen for when the download is done or if it fails
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
            Uri downloadUrl = taskSnapshot.getDownloadUrl(); //After uploading your picture you can get the download url of it

mDatabase.child("images").setValue(downloadUrl); //and then you save it in your database
            
        }
    });

and then just remember to get the downloadURL from the database like this:

mDatabase.addValueEventListener(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    String downloadURL = dataSnapshot.getValue(String.class);
    //do whatever you want with the download url
  }

Upvotes: 1

Related Questions