I created a table and database to store "title" "description" and "images use BLOB".But I can't retrieve image from SQLite database.I think my code is wrong.My Application shows new the description and images when click the next button. And Where I should keep all image in folder?
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";
}
Class 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();
}
}
error: incompatible types: String cannot be converted to Bitmap
replace this line:
word.setImage(c.getString(c.getColumnIndex(WordsContract.WordsTable.COLUMN_IMAGE)));
with:
word.setImage(c.getBlob(c.getColumnIndex(WordsContract.WordsTable.COLUMN_IMAGE)));
and to load image:
mImageView.setImageBitmap(BitmapFactory.decodeByteArray( currentWord.getImage(),
0,currentWord.getImage().length));
To store image in database, convert it into byte[]
public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, outputStream);
return outputStream.toByteArray();
}
To store in database use this :
public void saveImageIntoDB(int id , Bitmap bitmap ) {
byte[] imageBytes = getBitmapAsByteArray(bitmap);
insert.bindLong(1, id);
insert.bindBlob(2, imageBytes);
insert.executeInsert();
insert.clearBindings() ;
}
To retrieve from database :
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.