I have an image as byte[]
and I need to save this image in database. I use Active Android library for this purpose. I know that data type in db for this purpose should be BLOB. I know that byte[]
can't be stored directly and I know that it should serialized. But when I try to serialize it - problem arises.
I tried to convert byte[]
to String
and then isnsert it to database- it worked: I saved it in database. But then, when I get this string back from db and convert it back to array of bytes, this array is not equal to the first one so when I try to decode this array as an image I receive
skia: --- SkImageDecoder::Factory returned null.
My question is: what should I do to serialize byte array, save it into database, deserialize it back to byte array which will be equal to first one? Exactly steps I mean.
I really was searching how to solve this problem for a very long time and I kindly ask you not to share here links to Active Android documentation or examples of TypeSerializer
implementation for other classes - I saw that a lot of times.
What I need is exactly way how to solve this problem step by step.
EDIT
Present code of my TypeSerializer which give me opportunity to save byte array at database with Active Android
public class ByteArraySerializer extends TypeSerializer {
@Override
public Class<?> getDeserializedType() {
return byte[].class;
}
@Override
public Class<?> getSerializedType() {
return String.class;
}
@Override
public Object serialize(Object o) {
if ( o != null ) {
return new String((byte[]) o);
}
return null;
}
@Override
public Object deserialize(Object o) {
if ( o != null ) {
String str = (String) o;
return str.getBytes();
}
return null;
}
}