+ 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.
3 odpowiedzi
+ 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.
+ 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
+ 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;