英文:
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:
,
如何做到这一点?
我尝试过:
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:
,
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<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);
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论