图片网址未被推送到Firebase数据库

时间:2017-10-02 12:17:52

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

我有一个注册活动,要求用户提供图片。当从图库中选择图片时,我将图片推送到firebase存储,然后将下载URL推送到firebase数据库。图片不是推送到firebase存储,而是推送到firebase数据库。这是我的代码

cameraImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/jpeg");
                intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
                startActivityForResult(Intent.createChooser(intent, "Complete action using"), RC_PHOTO_PICKER);

            }
        });


        mFirebaseStorage = FirebaseStorage.getInstance();
        mChatPhotosStorageReference = mFirebaseStorage.getReference().child("profile_pic");

在我的OnActivityResult代码中如下

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



        if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK ) {
            filePath = data.getData();

            try {
                //getting image from gallery
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                //uploading the image
                StorageReference photoRef = mChatPhotosStorageReference.child("writedatabase");
                Log.i("Pritish", "onComplete: "+filePath);
                // Upload file to Firebase Storage
                photoRef.putFile(filePath)
                        .addOnSuccessListener(RegisterActivity.this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                                Uri downloadUrl = taskSnapshot.getDownloadUrl();

                                User user = new User(null, downloadUrl.toString());
                                mMessagesDatabaseReference.push().setValue(user);

                            }
                        });


                //Setting image to ImageView
                cameraImageButton.setImageBitmap(bitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
永远不会调用

addOnSuccessListener。有人可以帮助我。

1 个答案:

答案 0 :(得分:1)

从addOnSuccessListener参数中删除RegisterActivity.this