我如何在此活动本身中获取firebase数据库

时间:2018-02-11 14:06:57

标签: java android firebase firebase-realtime-database

我的个人资料活动代码如何在EditText和ImageView本身设置此数据库

for image:

mSetupImageBtn = (CircleImageView) findViewById(R.id.setupImageBtn);

代名:

mNameField = (EditText)findViewById(R.id.setupNameField);





 mStorageImage = FirebaseStorage.getInstance().getReference().child("Profile_Images");
    mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("users");

    mAuth = FirebaseAuth.getInstance();

    mSetupImageBtn = (CircleImageView) findViewById(R.id.setupImageBtn);

    mNameField = (EditText)findViewById(R.id.setupNameField);

    mSubmitBtn = (Button)findViewById(R.id.setupSubmitBtn);


    mSubmitBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startSetupAccount();
        }
    });

    mSetupImageBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent galleryIntent = new Intent();
            galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("image/e");
            startActivityForResult(galleryIntent,GALLERY_REQUEST);
        }
    });

}

private void startSetupAccount() {

    final String name = mNameField.getText().toString().trim();

    final String user_id = mAuth.getCurrentUser().getUid();

    if (!TextUtils.isEmpty(name) && mImageUri !=null){

        mProgress.setMessage(getString(R.string.fin));

        mProgress.show();

        StorageReference filepath = mStorageImage.child(mImageUri.getLastPathSegment());

        filepath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                @SuppressWarnings("VisibleForTests")

                String downloadUri = taskSnapshot.getDownloadUrl().toString();



                mDatabaseUsers.child(user_id).child("name").setValue(name);
                mDatabaseUsers.child(user_id).child("image").setValue(downloadUri);

                mProgress.dismiss();

                Intent mainIntent = new Intent(SetupActivity.this,Main2Activity.class);
                mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(mainIntent);
                finish();

            }
        });
    }
}

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

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

       mImageUri = data.getData();



        CropImage.activity(mImageUri)
                .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){
            Uri resultUri = result.getUri();

            mSetupImageBtn.setImageURI(resultUri);

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

1 个答案:

答案 0 :(得分:1)

要检索数据:

 mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("users").child(user_id);
 mDatabaseUsers.addValueEventListener(new ValueEventListener(){
 @Override
 public void onDataChange(DataSnapshot dataSnapshot) {
  String names=dataSnapshot.child("name").getValue().toString();
  String images=dataSnapshot.child("image").getValue().toString();
   Glide.with(ActivityNameHere.this).load(images).into(mSetupImageBtn);
   mNameField.setText(names);
 }

 @Override
public void onCancelled(FirebaseError firebaseError) {


 }
});

使用Glide库可以在ImageView中加载图像。