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

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

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中创建一个子节点内的子节点。,

如何做到这一点?

我尝试过:

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

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

但都不起作用。

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

这是bitMapToString:

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

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:

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

and

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

but neither of those work

  1. {
  2. String groupCode=this.getArguments().getString("groupNum");
  3. super.onActivityResult(requestCode, resultCode, data);
  4. if (resultCode == Activity.RESULT_OK)
  5. {
  6. Uri chosenImageUri = data.getData();
  7. Bitmap mBitmap = null;
  8. try {
  9. mBitmap = MediaStore.Images.Media.getBitmap(requireActivity().getContentResolver(), chosenImageUri);
  10. Map<String, Object> codes = new HashMap<>();
  11. codes.put(bitMapToString(mBitmap), true);
  12. DatabaseReference db=FirebaseDatabase.getInstance().getReference();
  13. DatabaseReference groupCodeRef = db.child("CODES").child(groupCode);
  14. 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:

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

答案1

得分: 1

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

  1. Map<String, Object> codes = new HashMap<>();
  2. codes.put("bitmap1", true);
  3. codes.put("bitmap2", true);
  4. codes.put("bitmap3", true);
  5. DatabaseReference db = FirebaseDatabase.getInstance().getReference();
  6. DatabaseReference groupCodeRef = db.child("CODES").child(groupCode);
  7. groupCodeRef.setValue(codes);

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

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

  1. db
  2. |
  3. --- CODES
  4. |
  5. --- 41947781
  6. |
  7. --- bitmap1: true
  8. |
  9. --- bitmap2: true
  10. |
  11. --- 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:

  1. Map&lt;String, Object&gt; codes = new HashMap&lt;&gt;();
  2. codes.put(&quot;bitmap1&quot;, true);
  3. codes.put(&quot;bitmap2&quot;, true);
  4. codes.put(&quot;bitmap3&quot;, true);
  5. DatabaseReference db = FirebaseDatabase.getInstance().getReference();
  6. DatabaseReference groupCodeRef = db.child(&quot;CODES&quot;).child(groupCode);
  7. 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:

  1. db
  2. |
  3. --- CODES
  4. |
  5. --- 41947781
  6. |
  7. --- bitmap1: true
  8. |
  9. --- bitmap2: true
  10. |
  11. --- 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:

确定