如何从Firebase数据库子级获取组件?

时间:2019-01-25 12:59:42

标签: android firebase firebase-realtime-database picasso

我正在尝试使用毕加索从Firebase数据库存储中获取图像。 图像存储在一个标记为profileimage的子级中,当尝试获取图像而不是给我下载网址时,它会发回这个com.google.android.gms.tasks.zzu@342b889

我认为错误可能来自以下代码行: String image = dataSnapshot.child("profileimage").getValue(String.class);

数据库结构:

"user": {
    "Exw3XA8p1lPODAi6M3muxRk6qVy1": {
      "profileimage": "com.google.android.gms.tasks.zzu@342b889",
    },

这是代码:

userRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

//错误发生在下面的行

 String image = dataSnapshot.child("profileimage").getValue().toString();
                       Toast.makeText(SetupActivity.this,"ImageValue! "+image,Toast.LENGTH_LONG).show();

                        Picasso.get()
                            .load("https://firebasestorage.googleapis.com/v0/b/bfitbeta-a4458.appspot.com/o/profile%20image%2FExw3XA8p1lPODAi6M3muxRk6qVy1jpg?alt=media&token=a4c2b8b4-848e-425c-b553-b334a258e2fa")
                            .fit()
                            .centerCrop()
                            .placeholder(R.drawable.profile)
                            .into(profileimage, new Callback() {
                                @Override
                                public void onSuccess() {

                                }

                                @Override
                                public void onError(Exception e) {
                                    System.out.println(e);
                                    Log.d("BFIT1", String.valueOf(e));
                                    e.printStackTrace();

                                    Toast.makeText(SetupActivity.this,"Error!",Toast.LENGTH_SHORT).show();

                                }
                            }
                            );
                }
//
                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode ==GALLARY_PICK && resultCode== RESULT_OK && data!=null)
        {
            Uri imageuri = data.getData();

            CropImage.activity(imageuri)
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .setAspectRatio(1, 1)
                    .start(this);

        }

        if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
            CropImage.ActivityResult result = CropImage.getActivityResult(data);
            if (resultCode == RESULT_OK) {

                loadingbar.setTitle("saving image");
                loadingbar.setMessage("please wait ....");
                loadingbar.show();
                loadingbar.setCanceledOnTouchOutside(true);
                Uri resultUri = result.getUri();
                final StorageReference filePath = Userprofileimageref.child(currentuserid + "jpg");

                filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onComplete(final Task<UploadTask.TaskSnapshot> task) {

                        if (task.isSuccessful())
                        {
                            Toast.makeText(SetupActivity.this,"Profile image stored successfully",Toast.LENGTH_SHORT).show();
                            final String downloadUrl = filePath.getDownloadUrl().toString();
                            userRef.child("profileimage").setValue(downloadUrl)
                                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                                        @Override
                                        public void onSuccess(Void aVoid) {
                                            if (task.isSuccessful())
                                            {
                                                Intent selfintent = new Intent(SetupActivity.this,SetupActivity.class);
                                                startActivity(selfintent);
                                                Toast.makeText(SetupActivity.this,"image saved successfully",Toast.LENGTH_SHORT).show();
                                                loadingbar.dismiss();

                                            }
                                            else
                                            {
                                                String message = task.getException().getMessage();
                                                Toast.makeText(SetupActivity.this,"Error occurred: "+ message,Toast.LENGTH_SHORT).show();
                                                loadingbar.dismiss();

                                            }

                                        }
                                    });
                        }

                    }
                });
            }else
            {
                Toast.makeText(SetupActivity.this,"image cannot be cropped",Toast.LENGTH_SHORT).show();
                loadingbar.dismiss();
            }

        }

    }

请帮助!

0 个答案:

没有答案