How to save an image to SQLite? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to save an image to SQLite?

I need an app that can store thousand of images to SQLite. Basically what needs to happen is that if I search for an itemcode, the picture of that item will appear on an imageview. The app must work offline so that is the reason why my first thought was to save it in SQLite. However, I've read that the cursor has a limit or something like that. Any recommendation will be helpful.

12th Apr 2019, 7:16 AM
ariamossar
ariamossar - avatar
3 Answers
+ 3
Just an idea, how about saving the images into a folder then save just the itemcode and filename in the database, when one search for an itemcode you'll know the filename to load. IMHO probably this is more efficient since there will be no need to load data from database each time we need to load an image, especially having known there are thousands of images. Never mind if it's not feasible according to your specifications.
12th Apr 2019, 9:49 AM
Ipang
+ 2
You can read your image into Byte object and then can insert it into the database. //image to byte File file = new File("image.jpg"); byte[] content = File.readAllBytes(file.toPath()) Later you can retrieve the data back from the database and convert it back to image. I've not much idea about converting byte object to image but this link should be helpful for that https://www.tutorialspoint.com/How-to-convert-Byte-Array-to-Image-in-java
12th Apr 2019, 8:00 AM
Шащи Ранжан
Шащи Ранжан - avatar
+ 1
Already sort of did that. Right now, the image that I'm saving to database is from the imageview. This is what I did: Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 0, baos); byte[] byteArray = baos.toByteArray(); return byteArray;
12th Apr 2019, 8:10 AM
ariamossar
ariamossar - avatar