1

I do not know why but I can not save my Object to FirebaseDatabase. Can someone help me?

My object:

public class ChatEntity {

    public static final String ENTITY_IDENTIFIER = "chats";

    private String id;
    private String chatTitle;
    private Map<String, ProfileEntity> users;
    private Map<String, ProfileEntity> administratorsUser;
    private Bitmap chatIcon;

    ... Getters and Setters ...

}

I'm saving this with:

FirebaseDatabase.getInstance().getReference(ChatEntity.ENTITY_IDENTIFIER).child(chat.getId()).setValue(chat);

And only this is saved:

{
  "1521122180142&Teste&tduWYxVHRVPVIx6Sv4p8fNwKKJi2" : {
    "chatTitle" : "Teste",
    "id" : "1521122180142&Teste&tduWYxVHRVPVIx6Sv4p8fNwKKJi2"
  }
}

The field chatIcon was null, so it's ok. But the two Maps has 1 and 2 entrys, and don't getting saved.

How can I save it?

2 Answers 2

1

As you might see, the Firebase database only takes the String objects that you try to save. Any other object is not guaranteed to be saved, specially the Bitmap one. If you want to save a Bitmap:

  • Use Firebase Storage
  • Get the Base64 String from the Bitmap.

To save the nested HashMap, you might want to call the database several times (one for each Map), or to convert the Maps' values into Strings with a / for each time you are editing a child (although both of them are very messy).

Sign up to request clarification or add additional context in comments.

2 Comments

So if I create a method that return a JSON string of that object, I can save that. But how can I restore it from database?
You just need to cast the dataSnapshot.getValue() to the object you want, always checking that it's compatible.
0

As per official documentation, the Bitmap is not a supported data type in Firebase Realtime database. Remember, as a general rule, I'd say, never use base64. You don't really need to base64-encode your image. If you want to store images, don't use Firebase Realtime database, use Firebase Storage.

In order to have your database populated with users and administratorsUser, change the type from those maps with ProfileEntity objects. If you want to use maps, you can use to set/update them directly on a DatabaseReference object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.