如何在房间持久性库中插入图像

如何在房间持久性库中插入图像

本文介绍了如何在房间持久性库中插入图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的android应用程序使用房间持久性库,现在我必须在数据库中插入图像.我为原始数据类型成功定义了@Entity.并通过转换器类,我存储了所有对象,日期,时间.现在,我必须存储图像.我无法理解我们如何定义列信息和实体,以及如何插入该数据以及如何从表中读取数据.

I am using room persistence library for my android application, Now I have to insert image in my db. I successfully define @Entity for the primitive data type. and also through converter class, i stored all object, date, time. Now I have to store Image. I am not able to understand how we define Column info and entity and how we insert that data as well as read data from the table.

插入单行的最大数据大小是多少? Android SQLite中一个字段中数据的最大和最小大小是多少?

What is the maximum size of data which inserted into the single row? What is max and min size of data in one field in Android SQLite?

推荐答案

通常不建议将图像数据存储到数据库中.但是,如果您的项目需要它,那么您可以这样做.

It is usually not recommended to store image data into the database.But however if it is required for your project then you can do so.

图像数据通常使用BLOB数据类型存储到db中,Room还提供对BLOB数据类型文档

Image data are usually stored into db using BLOB data type, Room also provide support for BLOB data type Documentation

您可以如下所述声明您的实体类以存储图像数据.

You can declare your entity class as mentioned below to store Image data.

@Entity(tableName = "test")
public class Test{

@PrimaryKey
@ColumnInfo(name = "_id")
private int id;

@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
private byte[] image;
}

这篇关于如何在房间持久性库中插入图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 12:35