无法从Firebase数据库获取数据

时间:2018-10-18 16:09:12

标签: android firebase firebase-storage

我正在尝试从Firebase获取图像URL,然后滑动显示图像。我了解如何显示图片,但是不确定如何获取图片的网址并在用户上传图片后显示它。

public class user_profile extends AppCompatActivity {

private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;

//Android Layout

private CircleImageView mDisplayImage;
private TextView mName;
private TextView mStatus;

private Button mStatusBtn;
private Button mImageBtn;

private ProgressDialog mProgressDialog;

private static final int GALLERY_PICK = 1;

private StorageReference mImageStorage;

private Context context = user_profile.this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_profile);

    mDisplayImage = (CircleImageView) findViewById(R.id.profile_picture);
    mName = (TextView) findViewById(R.id.profile_user_name);
    mStatus = (TextView) findViewById(R.id.profile_user_status);

    mStatusBtn = (Button) findViewById(R.id.profile_change_status_btn);
    mImageBtn = (Button) findViewById(R.id.profile_change_image_btn);
    mImageStorage = FirebaseStorage.getInstance().getReference();

    mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();

    String current_uid = mCurrentUser.getUid();

    mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(current_uid);
    mUserDatabase.keepSynced(true);

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

            String name = Objects.requireNonNull(dataSnapshot.child("name").getValue()).toString();
            String status = Objects.requireNonNull(dataSnapshot.child("status").getValue()).toString();
            String image = Objects.requireNonNull(dataSnapshot.child("image").getValue()).toString();

            mName.setText(name);
            mStatus.setText(status);

            if (!image.equals("default")) {

                Glide
                        .with(context)
                        .load()
                        .into(mDisplayImage);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

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

            String status_value = mStatus.getText().toString();

            Intent status_intent = new Intent(user_profile.this, change_status.class);
            status_intent.putExtra("status_value", status_value);
            startActivity(status_intent);

        }
    });

    mImageBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent galleryIntent = new Intent();
            galleryIntent.setType("image/*");
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(Intent.createChooser(galleryIntent, "Select Image"), GALLERY_PICK);
        }
    });

}

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

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

        String imageUri = data.getDataString();

        CropImage.activity(Uri.parse(imageUri))
                .setAspectRatio(1, 1)
                .start(this);
    }

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


            mProgressDialog = new ProgressDialog((user_profile.this));
            mProgressDialog.setTitle("Uploading");
            mProgressDialog.setMessage("Pleas Stand By");
            mProgressDialog.setCanceledOnTouchOutside(false);
            mProgressDialog.show();

            Uri resultUri = result.getUri();

            String current_user_id = mCurrentUser.getUid();

            StorageReference filepath = mImageStorage.child("profile_images").child(current_user_id + (".jpeg"));
            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().getStorage().getDownloadUrl().toString();
                        // dont know if this solution will work at 6:15 video


                        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(user_profile.this, "Succesful Upload", Toast.LENGTH_LONG).show();
                                }
                            }
                        });


                    } else {
                        Toast.makeText(user_profile.this, "Error Up Loading", Toast.LENGTH_SHORT).show();
                        mProgressDialog.dismiss();
                    }

                }
            });

        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
        }
    }

}}

我不确定如果有更好的联系方式请告知我,获取图像URL是否是执行此任务的最佳方法。

0 个答案:

没有答案