0

So I have seen many resources online and it's usually about using blob type to store my image in SQLite database table in android studio but I actually tried that out and was not sure how to do it.

I actually came across the concept of getting the path of the image and storing as resource id and then calling it in my main activity

This is what I have seen in terms of retrieving it (I am developing a quiz app and I need to retrieve the stored image in the database and display as an ImageView like my question.)

Is there any way someone can let me know how I can store the image in SQLite table in android studio and then retrieving it as such. I am aware that getImage() is the getter that will be declared in which the resource will be stored in that possibly the SQLite put command that is usually done to insert values using content values

    photoQuestion = (ImageView) findViewById(R.id.photoquestion);
    photoQuestion.setImageResource(PHOTO_QUESTION.getImage());

My code to insert data into the table, so far for my question (which is supposed to be the image) i have been testing it as string to be able to display and see if it is working but now i want to replace the first field of the table to image (from current string to image datatype instead)

    private void fillQuestions() {
            PeopleModeQuestions question1 = new PeopleModeQuestions("photo of john", "Father", "Mother", 1);
            addQuestions(question1); // question will be inserted in database everytime this method is called
    
            PeopleModeQuestions question2 = new PeopleModeQuestions("photo of mary", "Father", "Mother", 2);
            addQuestions(question2);
    
            PeopleModeQuestions question3 = new PeopleModeQuestions("photo of susan", "Sister", "Friend", 1);
            addQuestions(question3);
    
            PeopleModeQuestions question4 = new PeopleModeQuestions("photo of mark", "Sister", "Friend", 2);
            addQuestions(question4);
        }
    
        private void addQuestions(PeopleModeQuestions questions) {
            ContentValues values = new ContentValues();
            // using the getter method, get questions out of the object
            values.put(PeopleModeConstants.MemoryDementiaQuestions.MemoryDementia_Relatives_Column_Photo, questions.getPhoto());
            values.put(PeopleModeConstants.MemoryDementiaQuestions.MemoryDementia_Relatives_Column_Relationship1, questions.getRelationship1());
            values.put(PeopleModeConstants.MemoryDementiaQuestions.MemoryDementia_Relatives_Column_Relationship2, questions.getRelationship2());
            values.put(PeopleModeConstants.MemoryDementiaQuestions.MemoryDementia_Relatives_Column_CorrectRelationship, questions.getCorrectRelationship());
            db.insert(PeopleModeConstants.MemoryDementiaQuestions.MemoryDementia_Relatives_Table, null, values);
    }
Preeti
  • 1
  • 2
  • perhaps have a look at https://stackoverflow.com/questions/53954175/how-can-i-insert-image-in-a-sqlite-database/53954280#53954280 – MikeT Apr 28 '21 at 22:14
  • @MikeT unfortunately i have come across answers similar to that but without the full code of how i actually am support to implement it, i am not exactly sure how to do it. i am a newbie in android studio. in the link you sent, what exactly is the `your_image_as_a_byte_array` – Preeti Apr 28 '21 at 22:37
  • @MikeT I have been testing my quiz app by making my question (which is supposed to be the image) as a text, just to see if my app is working. so now i want to replace the text string which has been my question as the image. I have edited the section of the code into the main question where i have been doing the populating of the tables, you can take a look there – Preeti Apr 28 '21 at 22:40
  • `what exactly is the your_image_as_a_byte_array ?` To store an image you convert it to a byte[] . In the example you need to look at and understand what the `addImageFromPath` method is doing. In short the file's path is passed, the file is read into a buffer that buffer is the byte[]. i.e. image_as_a_byte_array **just represents** whatever the image and thus file; it's not actual code. – MikeT Apr 28 '21 at 23:01
  • you say *I have 4 images that will be stored in database*. Where will those images come from? If they are images that will be included in the app then if you put them in the assets raw folder then the example should work with your images. see https://developer.android.com/guide/topics/resources/providing-resources **NOTE** that the example decides whether to store the image as a BLOB (aka byte[]) or the path based upon the file size. If you store images that are reaching 1Mb you may have issues. If they exceed 2Mb you will very likely have issues at 4Mb you will have issues. – MikeT Apr 28 '21 at 23:16

1 Answers1

1

Could you store the images in the Drawable folder? I've got one app in particular that has about 50 images and it's worked well doing that. I know it can get a bit unwieldy, but that would be my first thought? Or do you have hundreds of images to pull out?

Programatically set an imageView:

myImage.setImageResource(getResources().getIdentifier("myImage3.png", "drawable", getPackageName()));

Tom Roe
  • 11
  • 4
  • I have 4 images that will be stored in database, so the image is the qn, then it has the options like the online standard quiz app codes, but imagine, replace a text question with an image. i was thinking about drawable but again i am not sure how to implement it. my main activity will call that field and display the image same concept as how it would be if it was a text. i have completed my quiz app and so far tested with the image field as a text question. solid code on how to implement would be helpful. could you share your code section that inserts the images into the database? – Preeti Apr 28 '21 at 21:58
  • OK, so if you copy and paste the images into the Drawables folder (inside Res), then you can set the images programmatically. I've amended my answer above to show the code I've used previously. – Tom Roe Apr 29 '21 at 15:59