taskSnapshot.getDownloadUrl() method not working

后端 未结 23 902
有刺的猬
有刺的猬 2020-12-08 08:03
private void uploadImageToFirebaseStorage() {
    StorageReference profileImageRef =
        FirebaseStorage.getInstance().getReference(\"profilepics/\" + System.cur         


        
23条回答
  •  抹茶落季
    2020-12-08 08:54

    You wont get the download url of image now using

    profileImageUrl = taskSnapshot.**getDownloadUrl**().toString();
    

    this method is deprecated.

    Instead you can use the below method

        uniqueId = UUID.randomUUID().toString();
        ur_firebase_reference = storageReference.child("user_photos/" + uniqueId);
    
        Uri file = Uri.fromFile(new File(mphotofile.getAbsolutePath()));
        UploadTask uploadTask = ur_firebase_reference.putFile(file);
    
        Task urlTask = uploadTask.continueWithTask(new Continuation>() {
            @Override
            public Task then(@NonNull Task task) throws Exception {
                if (!task.isSuccessful()) {
                    throw task.getException();
                }
    
                // Continue with the task to get the download URL
                return ur_firebase_reference.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener() {
            @Override
            public void onComplete(@NonNull Task task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    System.out.println("Upload " + downloadUri);
                    Toast.makeText(mActivity, "Successfully uploaded", Toast.LENGTH_SHORT).show();
                    if (downloadUri != null) {
    
                        String photoStringLink = downloadUri.toString(); //YOU WILL GET THE DOWNLOAD URL HERE !!!!
                        System.out.println("Upload " + photoStringLink);
    
                    }
    
                } else {
                    // Handle failures
                    // ...
                }
            }
        });
    

提交回复
热议问题