如何解决方法getDownloadUrl()

时间:2018-09-03 11:59:32

标签: android firebase firebase-realtime-database firebase-storage

Uri resultUri = result.getUri();
            String current_user_id= mCurrentUser.getUid();
            StorageReference filepath = mImageStorage.child("profile_image").child(current_user_id+".jpg");
            filepath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    if(task.isSuccessful()){

                        String download_url = task.getResult().getDownloadUrl().toString();

                        mUserDatabase.child("image").setValue(download_url).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if(task.isSuccessful()){
                                    mProgressDialog.dismiss();
                                    Toast.makeText(SettingsActivity.this,"Success upploading.",Toast.LENGTH_LONG).show();

                                }
                            }
                        });
                    }else {
                        Toast.makeText(SettingsActivity.this,"error on upploading.",Toast.LENGTH_LONG).show();                        }
                        mProgressDialog.dismiss();
                }
            });

2 个答案:

答案 0 :(得分:0)

您必须在addOnSuccessListener()之后添加getDownloadUrl(),然后在onSuccess()方法内获取url字符串。

答案 1 :(得分:0)

尝试一下:

filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        //Do what you need to do with the URL
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});