Anyone Please Help me. I am trying to create a dictionary application in which i want to save words ,their meanings and picture with every word. I am trying to save all the words,images and meanings in sqlite database. The problem is that when i click on any word in the listview it shows me the word and definition but the image field is blank.Here is my class in which i am creating my database.
public class DictionaryDataBase extends SQLiteOpenHelper {
byte[][] img = {
DbBitmapUtility.getBytes(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)),
DbBitmapUtility.getBytes(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)),
DbBitmapUtility
.getBytes(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)) };
public DictionaryDataBase(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"CREATE TABLE Dict_Table (id INTEGER PRIMARY KEY, word TEXT NOT NULL, definition TEXT NOT NULL, image BLOB NOT NULL);");
try {
new DictionaryDB(context).addEntry(words, def, db, img);
} catch (Exception e) {
Log.e("Error", e.toString());
}
}
and this is the add entry method:
public void addEntry(String[] word, String[] def, SQLiteDatabase db, byte[][] img) throws SQLiteException {
for (int i = 0; i < word.length; i++) {
db.execSQL("INSERT INTO Dict_Table VALUES (" + i + 1 + ", '" + word[i] + "', '" + def[i] + "', '" + img[i]
+ "');");
}
and this is the method which i am using to get the image:
public byte[] Getimage() throws SQLException {
dbhelper = new DictionaryDataBase(context);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor res = db.rawQuery("SELECT * FROM Dict_Table WHERE word='" + MainActivity.item + "'", null);//MainActivity.item is the item which was clicked in the list view
res.moveToFirst();
byte[] img = res.getBlob(res.getColumnIndex(IMG_COL));
if (!res.isClosed()) {
res.close();
}
return img;
}
DbBitmapUtility.java:
public class DbBitmapUtility {
// convert from bitmap to byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, stream);
return stream.toByteArray();
}
// convert from byte array to bitmap
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}
If you need any other information I will provide it to you but please help me in this case. Its since 2-3 days, I have been stuck here.
Aucun commentaire:
Enregistrer un commentaire