task.getResult()。getDownloadUrl()方法不起作用

时间:2019-01-02 15:58:40

标签: android

private void storeFirestore(@NonNull Task<UploadTask.TaskSnapshot> task, String user_name) 
{

    Uri download_uri;

    if(task != null) {

        download_uri = task.getResult().getDownloadUrl();

    } else {

        download_uri = mainImageURI;

    }

    Map<String, String> userMap = new HashMap<>();
    userMap.put("name", user_name);
    userMap.put("image", download_uri.toString());

    firebaseFirestore.collection("Users").document(user_id).set(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {

            if(task.isSuccessful()){

                Toast.makeText(SetupActivity.this, "The user Settings are updated.", Toast.LENGTH_LONG).show();
                Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
                startActivity(mainIntent);
                finish();

            } else {

                String error = task.getException().getMessage();
                Toast.makeText(SetupActivity.this, "(FIRESTORE Error) : " + error, Toast.LENGTH_LONG).show();

            }

            setupProgress.setVisibility(View.INVISIBLE);

        }
    });


}

getDownloadUrl在其下的红线不起作用,或者downloadUrl需要为其添加一种方法。

2 个答案:

答案 0 :(得分:1)

在Firebase Storage API版本16.0.1中。如果按以下方式使用,getDownloadUrl()方法将不起作用 download_uri = task.getResult().getDownloadUrl();

因此,为了使用getDownloadUrl()方法,我们需要使用存储引用,如下所示:


final UploadTask uploadTask = filepath.putFile(resultUri);

uploadTask.addOnSuccessListener(new OnSuccessListener<TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {
                                String download_url = uri.toString();
                            }
                        });
                    }
                });`

答案 1 :(得分:0)

尝试更换

task.getResult().getDownloadUrl()

作者

task.getResult().getStorage().getDownloadUrl()

GitHub link