0

I need this: enter image description here

I can upload video file successfully to Firebase Storage but I don't get the download url. Here is my upload code:

 Task<Uri> uriTask = 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();
                downloadurl = mStorageRef.getDownloadUrl().toString();
                return mStorageRef.child(name1).getDownloadUrl();
            }
     }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {

                if(task.isSuccessful())
                   downloadurl = task.getResult().toString();

                   Map<String,Object> user = new HashMap<>();
                   user.put("videoname",name1);
                   user.put("videolink",downloadurl);
                   firebaseFirestore.collection("videos").document(userID).collection("video").document().set(user).addOnSuccessListener(new OnSuccessListener<Void>() {
                     @Override
                     public void onSuccess(Void aVoid) {
                          Toast.makeText(getActivity(),"Uploaded Successfully",Toast.LENGTH_SHORT).show();
                          progressBar.setVisibility(View.GONE);
                      }
                    });
                   }
                 });
               }
             })
Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35
vamsi
  • 1
  • 1
  • `mStorageRef.getDownloadUrl().toString()` does not work here, as `getDownloadUrl()` returns a task. Only once that task completes do you get the actual download URL. The correct way to get the download URL is thus to add a completion listener to the task. See https://stackoverflow.com/questions/51056397/how-to-use-getdownloadurl-in-recent-versions – Frank van Puffelen Mar 28 '20 at 15:23

2 Answers2

1

Try this

private Uri uri;    //global variable

Inside onActivityResult() set uri when the user selects a video from phone

onActivityResult(int requestCode, int resultCode, Intent data)
uri = data.getData(); 

Finally getting thedownloadURL

final StorageReference ref = storageReference.child("firebaseFilePath");

    ref.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                    //now sUrl contains downloadURL
                    sUrl = uri.toString();

                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    }
            });


        }
    });
0
task.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            String fileUrl = uri.toString()
        }
    });

add on success listener to download url. If you want to get url you have to do it

Kasım Özdemir
  • 5,414
  • 3
  • 18
  • 35