在Firebase中创建一个子节点内的子节点。

huangapple go评论79阅读模式
英文:

creating a child inside a child in firebase

问题

I have a Realtime Database that has the values of a group code. I want that the codes will be like a firebase of its own, including bitmaps of images, something like that:

在Firebase中创建一个子节点内的子节点。,

如何做到这一点?

我尝试过:

databaseReference.child("CODES").child(groupCode).child(bitMapToString(mBitmap)).setValue(mBitmap);

databaseReference.child("CODES").child(groupCode).setValue(mBitmap);

但都不起作用。

这是我的代码,但它不起作用。如果我删除从映射部分到结尾的部分,它就会起作用,所以问题在于插入。

这是bitMapToString:

public String bitMapToString(Bitmap bitmap) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();
    String temp = Base64.encodeToString(b, Base64.DEFAULT);
    return temp;
}
英文:

I have a Realtime Database that has the values of a group code. I want that the codes will be like a firebase of its own, including bitmaps of images, something like that:

在Firebase中创建一个子节点内的子节点。,

how can I do that?

I tried to do:

databaseReference.child("CODES").child(groupCode).child(bitMapToString(mBitmap)).setValue(mBitmap);

and

databaseReference.child("CODES").child(groupCode).setValue(mBitmap);

but neither of those work

    {
        String groupCode=this.getArguments().getString("groupNum");
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK)
        {





                    Uri chosenImageUri = data.getData();

                    Bitmap mBitmap = null;
                    try {
                       mBitmap = MediaStore.Images.Media.getBitmap(requireActivity().getContentResolver(), chosenImageUri);
                       Map<String, Object> codes = new HashMap<>();
                        codes.put(bitMapToString(mBitmap), true);

                        DatabaseReference db=FirebaseDatabase.getInstance().getReference();
                        DatabaseReference groupCodeRef = db.child("CODES").child(groupCode);
                        groupCodeRef.setValue(codes);

this is the code i have, and it just doesnt work. if i remove the part from the map until the end it will work, so the problem is in the insertion

this is bitMapToString:

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();
        String temp = Base64.encodeToString(b, Base64.DEFAULT);
        return temp;
    }

答案1

得分: 1

如果您想在41947781节点下存储多个值,那么您需要在其下添加键值对。要使用Java实现此操作,请查看以下代码:

Map<String, Object> codes = new HashMap<>();
codes.put("bitmap1", true);
codes.put("bitmap2", true);
codes.put("bitmap3", true);

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference groupCodeRef = db.child("CODES").child(groupCode);
groupCodeRef.setValue(codes);

我还建议您将一个监听器附加到setValue()操作,以查看是否出现问题。

上述代码将创建以下结构:

db
|
--- CODES
     |
     --- 41947781
           |
           --- bitmap1: true
           |
           --- bitmap2: true
           |
           --- bitmap3: true

当然,我们也可以将这些代码添加到数组而不是映射中,但我建议您阅读以下资源:

英文:

If you want to store multiple values under the 41947781 node, then you have to add key-values pairs right under it. To achieve that using Java, please check the code below:

Map&lt;String, Object&gt; codes = new HashMap&lt;&gt;();
codes.put(&quot;bitmap1&quot;, true);
codes.put(&quot;bitmap2&quot;, true);
codes.put(&quot;bitmap3&quot;, true);

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference groupCodeRef = db.child(&quot;CODES&quot;).child(groupCode);
groupCodeRef.setValue(codes);

I also recommend you attach a listener to the setValue() operation, to see if something goes wrong.

The above code will create a structure that looks like this:

db
|
--- CODES
     |
     --- 41947781
           |
           --- bitmap1: true
           |
           --- bitmap2: true
           |
           --- bitmap3: true

It's true we could add the codes into an array rather then in a map, but I recommend you read the following resource:

huangapple
  • 本文由 发表于 2023年4月13日 17:13:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定