1

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;
    }
}
Igor Dotsenko
  • 85
  • 1
  • 8
  • A byte array is already technically serialized. The point of serialization is to get an object into byte array format. And a byte array is already in byte array format. :-) Maybe you should share some code so we can see what you're trying to do. – Doug Stevenson Mar 24 '16 at 17:10
  • In this situation serialization meant as converting object to the type which could be placed in database. Single byte can be stored easily, but byte array not. Please see code of my TypeSerializer. – Igor Dotsenko Mar 24 '16 at 17:20
  • If you're dealing with sqlite, you can put a byte array directly into a table column. – Doug Stevenson Mar 24 '16 at 18:30
  • Thank you for your answers, but I still did not solve this issue. I know that sqlite can do it, probably I describe my idea not clear enough: i mean that Active Android ORM can't do it directly - when I try to store byte array just with someByteArray.save() method - null is stored, not an array. – Igor Dotsenko Mar 25 '16 at 08:48

1 Answers1

1

I know that byte[] can't be stored directly

Incorrect. A byte[] can be stored directly into a BLOB column.

In JDBC, you'd set it by calling PreparedStatement.setBytes(), and get it by calling ResultSet.getBytes().

Any functional ORM should support mapping a BLOB column to byte[] fields.

For Active Android, see these links:

You may have to serialize byte[] to a java.sql.Blob.

Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247