After upload a file in Android Firebase Storage how get the file download Url? getDownloadUrl() not working

前端 未结 6 1767
無奈伤痛
無奈伤痛 2020-12-01 14:21

In my new android firebase project, I used com.google.firebase:firebase-storage:16.0.1 library.

I get the following Error:

I opened ano

相关标签:
6条回答
  • 2020-12-01 14:45

    That method has been deprecated on version 16.0.1 (check Firebase release notes) so you have to use

    StorageReference.getDownloadUrl()

    If you want to get them after uploading the file, then you must check their documentation here. It is already updated.

    0 讨论(0)
  • 2020-12-01 14:51

    taskSnapshot.getDownloadUrl() is deprecated so i recommend that in your addOnSuccessListener() method, you use your storageReference and call the getDownloadUrl() method in order to get the url of the file and you can do whatever you want with it. Hope it helps.

             mUploadTask = storageRef.putFile(file).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
    
                    // get the image Url of the file uploaded
                    storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            // getting image uri and converting into string
                            Uri downloadUrl = uri;
                           fileUrl = downloadUrl.toString();
    
    
                        }
                    });
    
                }
            });
    
    0 讨论(0)
  • 2020-12-01 14:52

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

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

    In this case is better to continue using addOnSuccessListener as you're using, but instead of using getDownloadUrl directly, you need to add a listener to that too.

    Now the method is inside taskSnapshot.getMetadata().getReference() like this

    filePath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                     taskSnapshot.getMetadata().getReference().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            // User uri here
                        }
                    });
                }
    
            });
    
    0 讨论(0)
  • 2020-12-01 15:01

    Firebase upadated their method so kindly update yourself use this method kindly:

    this is the basic line at which you feeling annoying, very simple justget the download path in this way

     StorageReference ref = storageReference.child("images/"+ UUID.randomUUID().toString());
      UploadTask  uploadTask = ref.putFile(filePath);
      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();
    
    
        ///here is your image url enjoy this 
    
              Toast.makeText(CloudStorageActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
    
            }
    
    
          } else {
    
    
            // Handle failures
            // ...
          }
        }
      });
    
    0 讨论(0)
  • 2020-12-01 15:07

    I had Found 2 solution for my issue.

    Firebase Google Documentation :

    //add file on Firebase and got Download Link
    filePath.putFile(imageUri).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();
            }
            return filePath.getDownloadUrl();
        }
    }).addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {
            if (task.isSuccessful()){
                Uri downUri = task.getResult();
                Log.d(TAG, "onComplete: Url: "+ downUri.toString());
            }
        }
    });
    

    Another solution!

    It's more easy and small than google Firebase documentation and I'll use it:

    filePath.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    Log.d(TAG, "onSuccess: uri= "+ uri.toString());
                }
            });
        }
    });
    
    0 讨论(0)
提交回复
热议问题