简体   繁体   中英

Firebase realtime database with linking images in the same object

Is it possible if you have class for example like this

public class UserInfo {
    String name;
    String age;
    String url;
}

From android you first upload image and this image is saved in firebase storage. In the next step you publish your name, age and from storage used last uploaded image and linked this image with name and age

For example

name: 'John'

age: 50

url: firebase url image

Create Model class,

   public class Model implements Serializable {
   public String name;
   public String age;
   public String photoUrl;

   public Model (){
   }

public Model (String name, String age,String photoUrl){
   this.name= name;
   this.age= age;
   this.photoUrl= photoUrl;
}
}  

Insert image in firebase storage,

  UploadTask uploadTask;
  uploadTask = imagePathReference.putBytes(dataNew);
  uploadTask.addOnFailureListener(new OnFailureListener() {
  @Override
  public void onFailure(@NonNull Exception exception) {
   Log.e("firebase ", " addOnFailureListener ");
  }
 }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

 @Override
 public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
 //taskSnapShot u will get the download url.
 }
 });

save the download url, Then in next step after getting name and age

    Model user = new Model (name,age,url);

Here in this object(user) you will have the all the data.Now save the object in realtime firebase table.

Yes. You just simply use Firebase Storage for uploading image, in callback you get a downloadable URL of that image. Then put all the values in the custom object and save that object in RealTime Database of Firebase.

I already used this type of task

try this code according to your requirenments,

  StorageReference riversRef = mStorageRef.child("posts/images" + userId + "/" + String.valueOf(System.currentTimeMillis()) + ".jpg");
                            riversRef.putBytes(bytes)
                                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                        @Override
                                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                            // Get a URL to the uploaded content
                                            Uri downloadUrl = taskSnapshot.getDownloadUrl();
                                            Log.d("downloadUrl", "" + downloadUrl);

                                            writeNewPost(userId, user.getUsername(), user.getProfilePic(), String.valueOf(downloadUrl), body);
                                        }
                                    })
                                    .addOnFailureListener(new OnFailureListener() {
                                        @Override
                                        public void onFailure(@NonNull Exception exception) {
                                            // Handle unsuccessful uploads
                                            // ...

                                        }
                                    }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                                    //calculating progress percentage
                                    double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();

                                    //displaying percentage in progress dialog
                                }
                            });

and the writeNewPost function is as follow -

private void writeNewPost(String userId, String username, String userImage, String img, String body) {
    // Create new post at /user-posts/$userid/$postid and at
    // /posts/$postid simultaneously
    String key = mDatabase.child(KeyTAG.TAG_POSTS).push().getKey();
    Post post = new Post(userId, username, body, img, userImage, key, DateTimeUtil.GetUTCdatetimeAsDate(),"image","noThumb");
    Map<String, Object> postValues = post.toMap();

    Map<String, Object> childUpdates = new HashMap<>();
    childUpdates.put("/" + KeyTAG.TAG_POSTS + "/" + key, postValues);

    mDatabase.updateChildren(childUpdates);
    mDialog.hide();

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM