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

时间:2019-06-18 05:59:48

标签: java android android-sqlite android-imageview

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

Class DatabaseHelper

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; }

Class WordsContact

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();
    }
}

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

2 个答案:

答案 0 :(得分:0)

替换此行:

 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));

答案 1 :(得分:0)

要将图像存储在数据库中,请将其转换为字节[]

public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, outputStream);       
    return outputStream.toByteArray();
}

要存储在数据库中,请使用:

public void saveImageIntoDB(int id , Bitmap bitmap ) {


        byte[] imageBytes = getBitmapAsByteArray(bitmap);

        insert.bindLong(1, id);
        insert.bindBlob(2, imageBytes);

        insert.executeInsert();
        insert.clearBindings() ;

    }

要从数据库检索:

public Bitmap retrieveImageFromDB(int i){

        String query = "select img  from table where imageid=" + i ;
        Cursor cursor = db.rawQuery(query, null);

        if (cursor.moveToFirst()){
            byte[] imgByte = cursor.getBlob(0);
            cursor.close();
            return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
        }
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }

        return null;
    }