我想将图像和文本上传到Firebase实时数据库。下面是我的代码
var newList : ArrayList<Bitmap> = arrayListOf()
.......
private fun insertSellerItemData(mAuth: FirebaseAuth?) {
val progressDialog = ProgressDialog(this@SellItemActivity)
progressDialog.setTitle("Image is Uploading...");
progressDialog.show()
val userId = FirebaseAuth.getInstance().currentUser?.uid
val item = Item(tv_title.text.toString(), newList)
databaseReference = FirebaseDatabase.getInstance().getReference("Items").child(userId)
databaseReference?.setValue(item)?.addOnCompleteListener { task ->
if (task.isSuccessful) {
Toast.makeText(application, "Data successfully added", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this@SellItemActivity, "You cannot register with this mobile number", Toast.LENGTH_SHORT).show()
}
}
}
项目
class Item(val title: String,val newList: ArrayList<Bitmap>) :Serializable{}
错误
Process: com.example.lyner.authentication, PID: 24574
com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead
修改
我在存储到Firebase之前将位图转换为Uri
val lt : ArrayList<Uri> = arrayListOf()
for(i in newList)
{
val list = convertBitmaptoUri(i)
lt.add(list)
}
val item = Item(tv_title.text.toString(), tv_desc.text.toString(), tv_price.text.toString(),
tv_category.text.toString(), dueDate, lt)
databaseReference = FirebaseDatabase.getInstance().getReference("Items").child(userId)
databaseReference?.setValue(item)?.addOnCompleteListener { task ->
if (task.isSuccessful) {
Toast.makeText(application, "Data successfully added", Toast.LENGTH_SHORT).show()
progressDialog.dismiss()
} else {
Toast.makeText(this@SellItemActivity, "You cannot register with this mobile number", Toast.LENGTH_SHORT).show()
progressDialog.dismiss()
}
}
错误
java.lang.StackOverflowError: stack size 8MB
at java.lang.reflect.Method.invoke(Native Method)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:123)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
2019-03-24 12:24:59.268 27163-27163/com.example.lyner.authentication E/AndroidRuntime: at com.google.android.gms.internal.firebase_database.zzkt.zzi(Unknown Source:253)
at com.google.android.gms.internal.firebase_database.zzkt.zzl(Unknown Source:0)
at com.google.android.gms.internal.firebase_database.zzku.zzm(Unknown Source:185)
答案 0 :(得分:0)
如果不先将其转换为字符串,就不能在实时数据库中存储位图对象。实时数据库并不容易存储二进制数据。可以考虑将原始二进制数据存储在Cloud Storage中,并将对该文件的引用存储在数据库中。
答案 1 :(得分:0)
当我尝试将Bitmap
转换为Uri
java.lang.StackOverflowError: stack size 8MB
为解决此问题,我将其转换为String
而不是Uri
val lt: ArrayList<String> = arrayListOf()
for (i in newList) {
val list = bitmapToString(i)
lt.add(list)
}
fun bitmapToString(bitmap: Bitmap): String {
val baos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos)
val b = baos.toByteArray()
val temp = Base64.encodeToString(b, Base64.DEFAULT);
return temp;
}
对于此错误
Item does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped
我为此修改了Item
类
data class Item(
val title: String = "",
val newList: ArrayList<String>? = arrayListOf()) : Serializable