I have a problem to get video download url after upload to Firebase Cloud Storage in Android. getDownloadUrl is not working [duplicate]

南楼画角 提交于 2021-01-29 16:21:44

问题


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);
                      }
                    });
                   }
                 });
               }
             })

回答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) {
                    }
            });


        }
    });



回答2:


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



来源:https://stackoverflow.com/questions/60900379/i-have-a-problem-to-get-video-download-url-after-upload-to-firebase-cloud-storag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!