gpt4 book ai didi

java - 从 SQLite 数据库添加和检索图像

转载 作者:太空宇宙 更新时间:2023-11-04 09:34:45 25 4
gpt4 key购买 nike

我创建了一个表和数据库来存储“标题”“描述”和“图像使用 BLOB”。但是我无法从 SQLite 数据库检索图像。我认为我的代码是错误的。单击下一步按钮时,我的应用程序显示新的描述和图像。我应该将所有图像保存在文件夹中的哪里?

数据库助手类

void onCreate(SQLiteDatabase db) {
this.db = db;
SQL_CREATE_WORD_TABLE = "CREATE TABLE " +
WordsContract.WordsTable.TABLE_NAME + " ( " +
WordsContract.WordsTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
WordsContract.WordsTable.COLUMN_GROUP_NAME + " TEXT, " +
WordsContract.WordsTable.COLUMN_NAME_WORD + " TEXT, " +
WordsContract.WordsTable.COLUMN_IMAGE + " BLOB " +
")";
db.execSQL(SQL_CREATE_WORD_TABLE);

fillWordsTable();

}
private void fillWordsTable() {
Word f1 = new Word("FRUIT","Grapes","grape.png");
addQuestion(f1);
Word f2 = new Word("FRUIT","Longon","longon.png");
addQuestion(f2);
}
private void addQuestion(Word word) {
ContentValues cv = new ContentValues();
cv.put(WordsContract.WordsTable.COLUMN_GROUP_NAME,word.getGroup());
cv.put(WordsContract.WordsTable.COLUMN_NAME_WORD,word.getNameWord());
cv.put(WordsContract.WordsTable.COLUMN_IMAGE,word.getImage());
db.insert(WordsContract.WordsTable.TABLE_NAME,null,cv);
}
public List<Word> getAllWords(){
List<Word> wordList = new ArrayList<>();
db = getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM "+ WordsContract.WordsTable.TABLE_NAME,null);
if(c.moveToFirst()){
do{
Word word = new Word();word.setNameWord(c.getString(c.getColumnIndex(WordsContract.WordsTable.COLUMN_NAME_WORD)));
word.setImage(c.getString(c.getColumnIndex(WordsContract.WordsTable.COLUMN_IMAGE)));
wordList.add(word);
} while (c.moveToNext());
}
c.close();
return wordList; }

类词联系方式

public static class WordsTable implements BaseColumns{
public static final String TABLE_NAME = "word_questions";
public static final String COLUMN_GROUP_NAME = "groups";
public static final String COLUMN_NAME_WORD = "words";
public static final String COLUMN_IMAGE = "images";
}

类MainActivity

public void onCreate(){
mImageView = (ImageView) findViewById(R.id.image_view);
WordDbHelper dbHelper = new WordDbHelper(this);

mWordList = dbHelper.getAllWords();

wordCountTotal = mWordList.size();
Collections.shuffle(mWordList);
showNextQuestion();}

public void showNextQuestion(){
if(wordCounter < wordCountTotal){
currentWord = mWordList.get(wordCounter);

titleGroup.setText(currentWord.getGroup());
nameWord.setText(currentWord.getNameWord());
mImageView.setImageBitmap(currentWord.getImage());

wordCounter++;

}else{
finish();
}
}

错误:不兼容的类型:字符串无法转换为位图

最佳答案

替换这一行:

 word.setImage(c.getString(c.getColumnIndex(WordsContract.WordsTable.COLUMN_IMAGE)));

与:

word.setImage(c.getBlob(c.getColumnIndex(WordsContract.WordsTable.COLUMN_IMAGE)));

并加载图像:

mImageView.setImageBitmap(BitmapFactory.decodeByteArray( currentWord.getImage(), 
0,currentWord.getImage().length));

关于java - 从 SQLite 数据库添加和检索图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56642564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com