.getDownloadUrl()(“ Android Studio”无法正常工作)

时间:2018-09-08 10:40:49

标签: android firebase firebase-storage

.getDownloadUrl()方法不起作用,其下方显示红线。

我几个月前一直在使用android studio,一个月前我开始了一个项目,在我的项目中遇到了这个错误。 原因是什么,我该如何解决? 请帮我...

这是错误:

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == GALLERY_PICK && resultCode == RESULT_OK){

        Uri imageUri = data.getData();

        final String current_user_ref = "messages/" + mCurrentUserId + "/" + mChatUser;
        final String chat_user_ref = "messages/" + mChatUser + "/" + mCurrentUserId;

        DatabaseReference user_message_push = mRootRef.child("messages")
                .child(mCurrentUserId).child(mChatUser).push();

        final String push_id = user_message_push.getKey();


        StorageReference filepath = mImageStorage.child("message_images").child( push_id + ".jpg");

        filepath.putFile(imageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

                if(task.isSuccessful()){
              // this code is not working (.getDownloadUrl)..

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


                    Map messageMap = new HashMap();
                    messageMap.put("message", download_url);
                    messageMap.put("seen", false);
                    messageMap.put("type", "image");
                    messageMap.put("time", ServerValue.TIMESTAMP);
                    messageMap.put("from", mCurrentUserId);

                    Map messageUserMap = new HashMap();
                    messageUserMap.put(current_user_ref + "/" + push_id, messageMap);
                    messageUserMap.put(chat_user_ref + "/" + push_id, messageMap);

                    mChatMessageView.setText("");

                    mRootRef.updateChildren(messageUserMap, new DatabaseReference.CompletionListener() {
                        @Override
                        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {

                            if(databaseError != null){

                                Log.d("CHAT_LOG", databaseError.getMessage().toString());

                            }

                        }
                    });


                }

            }
        });

    }

}

1 个答案:

答案 0 :(得分:1)

storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {

    }
});

此方法将起作用您已经定义了storageReference对象