I've tried to follow other answers for similar question and watched videos but got no results that really helps me. How do I get write a code that helps me store an image in SQLite and retrieving it?
So this is my code for inserting it into the database (Database.java)
public boolean insertFoodcourt(int id, String desc, String path){
SQLiteDatabase db = this.getWritableDatabase();
try {
FileInputStream fs = new FileInputStream(path);
byte[] imgbyte = new byte[fs.available()];
fs.read(imgbyte);
ContentValues contentvalues = new ContentValues();
contentvalues.put("foodcourtid", id);
contentvalues.put("foodcourtdesc", desc);
contentvalues.put("foodcourtimg", imgbyte);
db.insert("Foodcourt", null, contentvalues);
fs.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
Here's is me manually inserting the data into the database (MainPage.java)
db.insertFoodcourt(1, "Foodcourt 1", "@drawable/fc1");
And here is me trying to retrieve the image from the database (Database.java)
public Bitmap getFoodcourtImg (int id){
SQLiteDatabase db = this.getReadableDatabase();
Bitmap fcImg = null;
Cursor cursor = db.rawQuery("SELECT * FROM Foodcourt WHERE foodcourtid = ?", new String[]{String.valueOf(id)});
if(cursor.moveToNext()){
byte[] img = cursor.getBlob(cursor.getColumnIndex("foodcourtimg"));
fcImg = BitmapFactory.decodeByteArray(img, 0, img.length);
}
return fcImg;
}
The only problem is retrieving the image and trying to put it into an ImageView (of a CardView). Before I edited the path/"foodcourtimg", I used this code and was able to get the description so I know that the code works, it's only the image part where I have a problem with.
public String getFoodcourtDesc (int id){
SQLiteDatabase db = this.getReadableDatabase();
String fcDesc = null;
Cursor cursor = db.rawQuery("SELECT * FROM Foodcourt WHERE foodcourtid = ?", new String[]{String.valueOf(id)});
if(cursor.moveToNext()){
fcDesc = cursor.getString(cursor.getColumnIndex("foodcourtdesc"));
}
return fcDesc;
}
Help would be greatly appreciated. If TL;DR, the first 3 codes are in need of help. The method to insert an image into the database, me inserting the image (or the path in this case) and the method to pull out the image. Thanks!