taskSnapshot.getDownloadUrl() is deprecated

前端 未结 10 1454
时光说笑
时光说笑 2020-11-27 19:39

Until now, the way to get the url from file on Storage in Firebase, I used to do this taskSnapshot.getDownloadUrl, but nowadays is deprecated, which method I

10条回答
  •  臣服心动
    2020-11-27 20:28

    Just use a Task instead of ref.putFile(uriImage) .addOnSuccessListener(new OnSuccessListener() Nowadays, firebase references suggest using Uploadtask objects

    I've done it like this:

    UploadTask uploadTask;
            uploadTask = storageReferenceProfilePic.putFile(uriProfileImage );
    
            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 storageReferenceProfilePic.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                    if (task.isSuccessful()) {
                        progressBarImageUploading.setVisibility(View.GONE);
                        Uri downloadUri = task.getResult();
                        profileImageUrl = downloadUri.toString();
                        ins.setText(profileImageUrl);
                    } else {
                        // Handle failures
                        // ...
                    }
                }
            });
    

    Notice these lines in the above code:

    Uri downloadUri = task.getResult();
    profileImageUrl = downloadUri.toString();
    

    Now profileImageUrl contains something like "http://adressofimage" which is the url to acess the image

    Now you're free to use the String profileImageUrl however you wish. For e.g., load the url into an ImageView using Glide or Fresco libraries.

提交回复
热议问题