DBFlow(将图像另存为Blob)

时间:2015-11-17 07:56:51

标签: android android-studio orm

我一直试图保存我的图像(使用DBFlow将Blob作为Blob存入数据库) 我收到这样的错误..

Error:(90, 30) error: incompatible types
required: Blob
found:    byte[]

我使用了一些教程将图像转换为字节,并使用列blob将其保存到数据库。

 try {
            FileInputStream fileInputStream = new FileInputStream(imageURL);
            byte[] image = new byte[fileInputStream.available()];
            fileInputStream.read(image);

            ImageModel imageModel = new ImageModel();
            imageModel.latitude = "12345";
            imageModel.img = image;
            imageModel.save();

        } catch (IOException e) {
            e.printStackTrace();
        }

最后是我的ImageModel.class,

import com.raizlabs.android.dbflow.annotation.Column;
import com.raizlabs.android.dbflow.annotation.PrimaryKey;
import com.raizlabs.android.dbflow.annotation.Table;
import com.raizlabs.android.dbflow.data.Blob;
import com.raizlabs.android.dbflow.structure.BaseModel;

/**
 * Created by Galvez on 11/17/2015.
 */
@Table(databaseName = AppDatabase.dbName)
 public class ImageModel extends BaseModel {
    @Column
    @PrimaryKey(autoincrement = true)
    long getId;

    @Column
    String latitude;

    @Column
    Blob img;
}

应该是什么问题?我错误地将图像转换为blob吗?

1 个答案:

答案 0 :(得分:2)

你有正确的想法。 Blob类表示您要将BLOB用作基础数据库列类型。你认为字节数组是在Blob中存储数据的方法是正确的。您只有一个小实现问题:Blob对象充当字节数组的包装器。在Java中,您无法将byte[]强制转换或强制转换为Blob;你需要使用Blob对象的方法。

所以上面的代码行应该是

imageModel.img = new Blob(image);

要恢复图像数据,您可能会执行类似

的操作
byte[] imageData = imageModel.img.getBlob();
Bitmap image = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
相关问题