Firebase how to get Image Url from firebase storage?

后端 未结 12 580
悲&欢浪女
悲&欢浪女 2020-12-23 15:15

Right now i am fetching image from Storage of Firebase by using below code :

mStoreRef.child("photos/" + model.getBase64Image())
          .getDownl         


        
相关标签:
12条回答
  • 2020-12-23 15:33

    Yes that's possible !!. Instead of some creepy complicated lines of code here is my shortcut trick to get that downloadurl from firebase storage in kotlin

    Note:Based on the latest firebase release there is no getdownloadurl() or getresult() method (they are the prey of deprecition this time around)

    So the the trick i have used here is that by calling UploadSessionUri from the taskSnapshot object which in turn returns the downloadurl along with upload type,tokenid(one which is available for only shorter span of time) and with some other stuffs.

    Since we need only download url we can use substring to get downloadurl and concat it with alt=media in order 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 it helps !.

    0 讨论(0)
  • 2020-12-23 15:34

    that's how I'm getting download link in kotlin android.

     ref.putFile(filePath!!)
                .addOnSuccessListener {
    
                    val result = it.metadata!!.reference!!.downloadUrl;
                    result.addOnSuccessListener {
    
                       var imageLink = it.toString()
    
    
                    }
                }
    
    0 讨论(0)
  • 2020-12-23 15:35

    If you already have download infrastructure based around URLs, or just want a URL to share, you can get the download URL for a file by calling the getDownloadUrl() method on a storage reference.

    // Create a storage reference from our app
    val storageRef = storage.reference
    
     storageRef.child("users/me/profile.png").downloadUrl.addOnSuccessListener {
                // Got the download URL for 'users/me/profile.png'
            }.addOnFailureListener {
                // Handle any errors
            }
    

    firebase documentation

    0 讨论(0)
  • 2020-12-23 15:41

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

    It should be used this way:

    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            String image = taskSnapshot.getDownloadUrl().toString());
        }
    });
    
    0 讨论(0)
  • 2020-12-23 15:42

    The above method taskSnapshot.getMetadata().getDownloadUrl(); is deprecated and as a substitute provided this alternative:

    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
            // ...
        }
      }
    });
    
    0 讨论(0)
  • 2020-12-23 15:43
     //kotlin
    
    var uri:Uri
        uploadTask.addOnSuccessListener {t ->
            t.metadata!!.reference!!.downloadUrl.addOnCompleteListener{task ->
                uri = task.result!!
    
            }
        }
    
    0 讨论(0)
提交回复
热议问题