0

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!

Rahul Chokshi
  • 670
  • 4
  • 18
Jarod Tan
  • 25
  • 5
  • 1
    FWIW, I do not recommend storing images or other large blobs in SQLite. Android's API to SQLite does not handle large result sets very well, and images tend to be large. Beyond that, though, it is unclear what the problem is. You have code that returns a `Bitmap`. That code has bugs (e.g., you are not closing the `Cursor`, you are not handling the case where there are no results), but it should basically work. What specific problem are you running into? – CommonsWare Aug 18 '18 at 18:20
  • I agree with @CommonsWare . You can use cache or store image locally in a folder. – Sanjay Sharma Aug 18 '18 at 18:25
  • To clarify mTak's answer, `"@drawable/fc1"` is not a file. Resources are files on your development machine; they are not files on the device. Your code might work if you used it with files (e.g., an image that you downloaded from somewhere). – CommonsWare Aug 18 '18 at 18:43
  • @CommonsWare Hi, I think your answer gave me an enlightenment to the proper answer I should be asking. And thank you for that. How do transfer the image from the project file (of Android Studio) into the emulator's SQLite Database? I want to get the image from the SQLite Database when displaying it in the emulator and not from the project file's drawable to display in the ImageView. I acknowledge it is a bit redundant but I'm running this project on several computers. – Jarod Tan Aug 19 '18 at 20:16
  • @JarodTan: "How do transfer the image from the project file (of Android Studio) into the emulator's SQLite Database?" -- I do not recommend that. "I want to get the image from the SQLite Database when displaying it in the emulator and not from the project file's drawable to display in the ImageView" -- I do not recommend that either. – CommonsWare Aug 19 '18 at 20:29
  • @CommonsWare Hi, thank you for the quick respond. Alright, I'll take your advice and mTak's advice. Thanks again so much! – Jarod Tan Aug 19 '18 at 20:34

2 Answers2

2

Why are you trying to save a drawable in a database that is already stored in your app?
You can just save its name as a string: "fc1" and retrieve it as a string, say in a variable name.
After that by:

int id = resources.getIdentifier(name, "drawable", context.getPackageName());

you get its id and you get the drawable:

Drawable d = getResources().getDrawable(id);

and you can use it as you like.

  • There is no drawable resource cited in the question. – CommonsWare Aug 18 '18 at 18:38
  • @CommonsWare and what is this: `"@drawable/fc1"`? –  Aug 18 '18 at 18:39
  • Ah, my apologies! – CommonsWare Aug 18 '18 at 18:42
  • @mtak Hi I'm new to Android and I'm not too clear. Is "drawable/fc1.png" in the Android Studio Project Folder considered to be in the database of the phone? Because whilst I was searching the .db files of the phone, I did not come across image files in the "drawable" folder that exists in the phone/simulator. My assumption is that SQLite Database (.db files) is the phone's database, while the files in the project is specifically from the computer/project and does not save into the phone. Do correct me if I'm wrong. – Jarod Tan Aug 19 '18 at 20:26
  • @JarodTan this drawable and all drawables that you have in `res/drawable` folder are stored in the app itself so when you install the app it will be stored in the phone but not as an individual file. Your app can access it. –  Aug 19 '18 at 20:30
  • @mTak Thank you for the quick respond. I see, was previously mistaken. Again, thanks a lot! – Jarod Tan Aug 19 '18 at 20:35
0

I have one solution instead of save byte[] in db try to store image in internal memory of your application directory Check here and then save the image path in your table.