Firebase how to get image url from firebase store?

Now I get the image from the Firebase repository using the code below:

mStoreRef.child("photos/" + model.getBase64Image()) .getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { // Got the download URL for 'photos/profile.png' } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors Toast.makeTextthis, "image not dowloaded", Toast.LENGTH_SHORT).show(); } }); 

How to get this URL?

You can get this URL as shown.

+11
source share
7 answers

Follow this link - https://firebase.google.com/docs/storage/android/download-files#download_data_via_url

  storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { // Got the download URL for 'users/me/profile.png' 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 } }); 
+15
source

In the past, getMetadata().getDownloadUrl() used getMetadata().getDownloadUrl() , and today they use getDownloadUrl()

This should be used like this:

 .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { String image = taskSnapshot.getDownloadUrl().toString()); } }); 
+2
source

As per recent Firebase changes, here is the updated code:

File file = new File(String.valueOf(imageUri));
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference().child("images");

  storageRef.child(file.getName()).putFile(imageUri) .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { pd.dismiss(); Toast.makeText(MyProfile.this, "Image Uploaded Successfully", Toast.LENGTH_SHORT).show(); Task<Uri> downloadUri = taskSnapshot.getStorage().getDownloadUrl(); if(downloadUri.isSuccessful()){ String generatedFilePath = downloadUri.getResult().toString(); System.out.println("## Stored path is "+generatedFilePath); }} }) .addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { pd.dismiss(); } }); } 
+2
source

The above taskSnapshot.getMetadata().getDownloadUrl(); method taskSnapshot.getMetadata().getDownloadUrl(); Deprecated and provided this alternative as a replacement :

 final StorageReference ref = storageRef.child("images/mountains.jpg"); uploadTask = ref.putFile(file); Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() { @Override public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception { if (!task.isSuccessful()) { throw task.getException(); } // Continue with the task to get the download URL return ref.getDownloadUrl(); } }).addOnCompleteListener(new OnCompleteListener<Uri>() { @Override public void onComplete(@NonNull Task<Uri> task) { if (task.isSuccessful()) { Uri downloadUri = task.getResult(); } else { // Handle failures // ... } } }); 
+1
source

this is how i get the download link in kotlin android.

  ref.putFile(filePath!!) .addOnSuccessListener { val result = it.metadata!!.reference!!.downloadUrl; result.addOnSuccessListener { var imageLink = it.toString() } } 
+1
source
  //kotlin var uri:Uri uploadTask.addOnSuccessListener {t -> t.metadata!!.reference!!.downloadUrl.addOnCompleteListener{task -> uri = task.result!! } } 
0
source

Yes it is possible! Instead of some creepy complex lines of code here is my shortcut to get this downloadurl from the Firebase repository in kotlin

Note : based on the latest version of Firebase, there is no getdownloadurl () or getresult () method ( this time they are obsolete)

So, the trick I used here is that by calling UploadSessionUri from the taskSnapshot object, which in turn returns the downloadurl along with the type of download, tokenid (one that is only available for a shorter amount of time) and with some other things.

Since we only need to download the URL, we can use the substring to get the downloadurl and combine it with alt = media to view the image.

 var du:String?=null var du1:String?=null var du3:String="&alt=media" val storage= FirebaseStorage.getInstance() var storagRef=storage.getReferenceFromUrl("gs://hola.appspot.com/") val df= SimpleDateFormat("ddMMyyHHmmss") val dataobj= Date() val imagepath=SplitString(myemail!!)+"_"+df.format(dataobj)+".jpg" val imageRef=storagRef.child("imagesPost/"+imagepath) val baos= ByteArrayOutputStream() bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos) val data=baos.toByteArray() val uploadTask=imageRef.putBytes(data) uploadTask.addOnFailureListener{ Toast.makeText(applicationContext,"Failed To Upload", Toast.LENGTH_LONG).show() }.addOnSuccessListener { taskSnapshot -> imageRef.downloadUrl.addOnCompleteListener (){ du=taskSnapshot.uploadSessionUri.toString() du1=du!!.substring(0,du!!.indexOf("&uploadType")) downloadurl=du1+du3 Toast.makeText(applicationContext,"url"+downloadurl, Toast.LENGTH_LONG).show() } } 

Hope this helps!

0
source

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


All Articles