将数据和图像作为单个节点添加,并使用firebase检索它

时间:2017-02-13 08:51:27

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

我使用以下代码从应用程序发送数据和图像,但数据和图像将分开进行。我的问题是我想发送一种包含信息和上传图片的用户表单。

以下是代码 -

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




tvname= (TextView) findViewById(R.id.tvname);
tvnumber= (TextView) findViewById(R.id.tvnumber);
tvage= (TextView) findViewById(R.id.tvage);
tvdata= (TextView) findViewById(R.id.tvdata);

etname = (EditText)findViewById(R.id.etname);
etnumber = (EditText)findViewById(R.id.etnumber);
etage = (EditText)findViewById(R.id.etage);

btn = (Button) findViewById(R.id.button2);
btnimage = (Button) findViewById(R.id.selectimage);

mDatabase = FirebaseDatabase.getInstance().getReference();
FirebaseStorage storage = FirebaseStorage.getInstance();

mProgressDialog = new ProgressDialog(this);


mStorage = FirebaseStorage.getInstance().getReference();

btnimage.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        startActivityForResult(intent, GALLERY_INTENT);
    }


});


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


            String name = etname.getText().toString();
            String number = etnumber.getText().toString();
            String age = etage.getText().toString();

            User user = new User();
            user.setUsername(name);
            user.setNumber(number);
            user.setAge(age);
            name = name.trim();
            number = number.trim();
            age = age.trim();
//              User user = new User(user, number, age);
            mDatabase.child("users").child(name).setValue(user);



            myRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    for (DataSnapshot postSnapshot : snapshot.getChildren())    {
                        User user = postSnapshot.getValue(User.class);
                        String string = "Name: " +user.getUsername() + "\n Number : " +user.getNumber() + "\n Age" +user.getAge();
                        tvdata.setText(string);
                    }
                }





                @Override
                public void onCancelled(DatabaseError databaseError) {
                    System.out.println("The read failed: " + databaseError.getMessage());
                }

            });

        }
    });



}


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

         if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
             mProgressDialog.setMessage("Uploading");
             mProgressDialog.show();
             Uri uri = data.getData();

             StorageReference filepath = mStorage.child("Photos").child(uri.getLastPathSegment());
             filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                 @Override
                 public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                     Toast.makeText(MainActivity.this, "Upload Done", Toast.LENGTH_SHORT).show();
                     mProgressDialog.dismiss();
                 }
             });

         }

     }
}

我发送下面的数据是检索数据的代码 -

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

if (!FirebaseApp.getApps(this).isEmpty()) {
    // Not in crash process.  Do your Remote Config init here.
    System.out.println("ss");
}

tv = (TextView) findViewById(R.id.textView3);
tv1 = (TextView) findViewById(R.id.textView2);
mDatabase = FirebaseDatabase.getInstance().getReference();
btn = (Button) findViewById(R.id.button2);



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

        ValueEventListener valueEventListener = myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                    User user = postSnapshot.getValue(User.class);
                  //  String string = "Name: " + user.getUsername() + "\n Number : " + user.getNumber() + "\n Age: " + user.getAge();
                    String string = tv.getText() + "\nName: " + user.getUsername() + "\n Number : " + user.getNumber() + "\n Age: " + user.getAge() +"\n";

                    tv.setText(string);

                }
            }

以下是我的疑问 -

  1. 如何发送数据和图像并将其存储在firebase上的同一节点中

  2. 在检索数据时,我希望不同的文本视图中显示不同的节点数据以及从firebase接收的相应图像

  3. 下面的截图是为了让事情更加清晰

    Send Data

    Retrieve Data

    提前感谢您的帮助

0 个答案:

没有答案