0

I would like to save uploaded file path to database. For this, Firebase gives me URI of uploaded file : taskSnapshot.getDownLoadUrl() . Now I need to save this to database for later use. What is the the correct way to save it as ? :

taskSnapshot.getDownLoadUrl().toString or taskSnapshot.getDownLoadUrl().getPath()?

For testing I tried to load (Glide) the uploaded image file into an imageview using these strings above as load() method parameter. toString works but getPath doesn't.

2 Answers 2

1

When you get the downloadUrl of the taskSnapshot it returns an uri, if you then use toString() you get a string that contains the download url of the image, but if you ask for the path, it will return nothing, since the uri returned by taskSnapShot.getDownloadUrl() is not hierarchical. If you want the file path on firebase storage to access it via storage reference and not URL, you can use taskSnapshot.getStorage().

Sign up to request clarification or add additional context in comments.

1 Comment

Note that you actually have two options when storing this data into the database. You can: a) Store the 'unguessable download url' that you describe via getDownloadUrl().toString(). b) Store the gs:// uri (gs://bucket/path/to/object.png). The advantage of the second option is that it remains secure if someone shares it. You can use this gs:// uri to create a new StorageReference and then do a secure download. Whereas option a) allows unsecure downloads for whoever you share the url with.
0

From Firebase documentation:

getDownloadUrl String A URL that can be used to download the object. This is a public unguessable URL that can be shared with other clients. This value is populated once an upload is complete. 

You should getDownloadUrl and save it into Uri, then you can use Uri.tostring with Glide

 Uri downloadUrl = taskSnapshot.getDownloadUrl(); 

source

Another option, its to use FirebaseUI Storage, you should save the storageReference and then you can use it with

 Glide.with(this /* context */) .using(new FirebaseImageLoader()) .load(storageReference) .into(imageView); 

Comments