[英]Error to get Bitmap from FirebaseDatabase on Android
I have a class that have a Bitmap object, and when I setValue(MyClass.class) to FirebaseDatabase I discover that is possible! 我有一个具有Bitmap对象的类,当我将Value(MyClass.class)设置为FirebaseDatabase时,我发现这是可能的! And the RealtimeDatabase save the properties of Bitmap image. 并且RealtimeDatabase保存位图图像的属性。 The problem occurs when I read the value, the follow error happened: 当我读取该值时出现问题,发生跟随错误:
com.google.firebase.database.DatabaseException: Class android.graphics.Bitmap is missing a constructor with no arguments
but I can't Override the constructor of Bitmap image. 但是我不能覆盖位图图像的构造函数。 How can I get around this situation? 我如何解决这种情况?
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterator<DataSnapshot> iterator = dataSnapshot.getChildren().iterator();
log("Init ValueEventListener onDataChange()");
while( iterator.hasNext() ) {
DataSnapshot data = iterator.next();
log("Data : " + data.getKey());
Treino t = data.getValue(Treino.class);
}
}
}
public class Treino implements Serializable{
String nome;
List<Exercicio> listaExercicios;
List<Integer> listaDiasSemana;
String hora; // hh:mm
public Treino(String nome, List<Exercicio> listaExercicios, List<Integer> listaDiasSemana, String hora) {
this.nome = nome;
this.listaExercicios = listaExercicios;
this.listaDiasSemana = listaDiasSemana;
this.hora = hora;
}
public class Exercicio implements Serializable {
String nome, tipo, nomeImagem;
Bitmap imagem;
public Exercicio() {
}
public Exercicio(String nome, Bitmap imagem, String nomeImagem) {
this.nome = nome;
this.imagem = imagem;
this.nomeImagem = nomeImagem;
}
I omitted the getters and setters. 我省略了getter和setter。
You must convert Bitmap to String 您必须将位图转换为字符串
This is code 这是代码
public String BitMapToString(Bitmap bitmap){
ByteArrayOutputStream ByteStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100, ByteStream);
byte [] b=ByteStream.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);
return temp;
}
And Exercicio.class 和Exercicio.class
public class Exercicio implements Serializable {
String nome, tipo, nomeImagem;
String bitmapImageString;
public Exercicio() {
}
public Exercicio(String nome, String imagem, String nomeImagem) {
this.nome = nome;
this.bitmapImageString = imagem;
this.nomeImagem = nomeImagem;
}
}
When get String from server. 从服务器获取字符串时。 You must convert String to Bitmap 您必须将String转换为Bitmap
public Bitmap StringToBitMap(String encodedString){
try{
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
HOPE! 希望! Help you! 帮你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.