简体   繁体   English

如何使用活动的android序列化字节数组并将其存储到数据库中?

[英]How to serialize byte array and store it into database using active android?

I have an image as byte[] and I need to save this image in database. 我有一个图像作为byte[] ,我需要将此图像保存在数据库中。 I use Active Android library for this purpose. 我为此使用Active Android库。 I know that data type in db for this purpose should be BLOB. 我知道db中用于此目的的数据类型应该是BLOB。 I know that byte[] can't be stored directly and I know that it should serialized. 我知道byte[]不能直接存储,我知道它应该序列化。 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. 我试图将byte[]转换为String然后将其插入数据库 - 它工作:我将它保存在数据库中。 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 但是然后,当我从db取回该字符串并将其转换回字节数组时,该数组不等于第一个字节,因此当我尝试将此数组解码为图像时收到

skia: --- SkImageDecoder::Factory returned null. skia:--- SkImageDecoder :: Factory返回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. 我真的在寻找如何解决这个问题很长一段时间,我恳请你不要在这里分享Active Android文档的链接或其他类的TypeSerializer实现的例子 - 我看了很多次。 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 我的TypeSerializer的当前代码,使我有机会使用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;
    }
}

I know that byte[] can't be stored directly 我知道byte []不能直接存储

Incorrect. 不正确。 A byte[] can be stored directly into a BLOB column. byte[]可以直接存储到BLOB列中。

In JDBC, you'd set it by calling PreparedStatement.setBytes() , and get it by calling ResultSet.getBytes() . 在JDBC中,您可以通过调用PreparedStatement.setBytes()设置它,并通过调用ResultSet.getBytes()获取它。

Any functional ORM should support mapping a BLOB column to byte[] fields. 任何功能性ORM都应支持将BLOB列映射到byte[]字段。

For Active Android, see these links: 对于Active Android,请参见以下链接:

You may have to serialize byte[] to a java.sql.Blob . 您可能必须将byte[]序列化为java.sql.Blob

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM