如何在android中使用sqlite填充图像数据库?

时间:2015-01-21 06:46:42

标签: android sqlite

我想创建一个学习应用程序.IT包含一个图像和四个可能的答案,其中一个是真的......我创建了3个表 图像表与word,图像和word id是主键 具有单词id和该单词复杂性的复杂性表 具有字ID和簇号的簇表

现在我如何使用系统中的图像填充图像表并访问图像视图上的图像

public class Database extends SQLiteOpenHelper
{
   private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "kid.db";

public Database(Context context)
{
    super(context,DATABASE_NAME,null,DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase)
{
    final String SQL_CREATE_IMAGE_TABLE = "create table image(word_ID integer primary key,word text not null,img blob);";

    final String SQL_CREATE_COMPLEXITY_TABLE = "create table compexity(WORD_ID integer primary key,complexity integer not null);";

    final String SQL_CREATE_CLUSTER_TABLE = "create table cluster(WORD_ID integer primary key,cluster integer not null);";

    sqLiteDatabase.execSQL(SQL_CREATE_IMAGE_TABLE);
    sqLiteDatabase.execSQL(SQL_CREATE_COMPLEXITY_TABLE);
    sqLiteDatabase.execSQL(SQL_CREATE_CLUSTER_TABLE);

}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion)
{


}

}

2 个答案:

答案 0 :(得分:1)

Cursor c = db.query(your query statment);
if(c != null && c.getCount()!=0){
byte[] blob = c.getBlob(c.getColumnIndex(YourDB.ColumnName));
ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
}

将此位图设置为ImageView。 还要检查您的图片列必须是BLOB类型。

答案 1 :(得分:0)

您可以在表格的image列中放置一个byte []。

示例:向IMAGE_TABLE添加行

public void addEntry( String word, byte[] image) throws SQLiteException{
ContentValues cv = new  ContentValues();
cv.put(KEY_NAME,    word);
cv.put(KEY_IMAGE,   image);
database.insert( DB_TABLE, null, cv );