0

My question is hard to explain. In my application, there is a custom object I've made. In this object, is a date timestamp. Also in this object are several photos (or references to photos?) that the user can take with the camera hardware. So I need the ability to store several pictures along with this shared timestamp.

My question is how this information gets stored in a SQLite database, since I want this information to persist. I don't think storing the actual images in the database are a good idea necessarily, but I don't know what kind of references you'd store. Would you just store the path to the pictures on the filesystem in the database? What do you actually store? What if someone decides to save their data to an SD card instead of internal storage, etc? What is the typical way this sort of thing is handled / is there something built-in to Android/SQlite that handles this situation?

KaliMa
  • 1,970
  • 6
  • 26
  • 51
  • You say 'Also in this object are several photos (or references to photos?)'. How are the photos stored in your object exactly? An URL?, a file-path? A byte array? Perhaps it would be good to add some of your code to your question. – Ridcully Apr 07 '16 at 14:23
  • @Ridcully I don't have any code yet for the photo stuff because I am still in the planning stage on that point. I'm just trying to figure out what I can/should be implementing in terms of typical/best practices. – KaliMa Apr 07 '16 at 14:24
  • For example maybe it is prudent to have an array of Bitmaps in each object, but maybe most people avoid this because it uses a lot of memory/resources, so maybe they store some other file handle instead and only invoke pictures when needed, and so on. I'm not sure how this situation is normally handled. – KaliMa Apr 07 '16 at 14:26
  • I see, but I'm afraid in that case you question is way off topic, as Stackoverflow is here to help with specific programming questions. Asking 'what is the best way to do this or that?' is _not_ in the scope of this site. – Ridcully Apr 07 '16 at 14:26
  • This is a programming question, though. I'm asking how picture information is stored in conjunction with sqlite3 – KaliMa Apr 07 '16 at 14:27

3 Answers3

1

save the image to your phone memory or sd card and save only the path to your sqlite database. that will be much better than storing the image

dione llorera
  • 357
  • 4
  • 14
  • I use an sqlite database associated with my application. I am asking what I store in the database, since these objects get parsed/logged into tables. – KaliMa Apr 07 '16 at 14:19
  • just store the path to your image in your database – dione llorera Apr 07 '16 at 14:21
  • @KaliMa not quite sure why the downvote ... by path, it would be a `String` reference that could be parsed as a `Uri` when retrieving. If you were to store an image in a SQLite database it would be a `BLOB` column example here : http://stackoverflow.com/a/9309315/4252352 - however, if as you say it could be quite a few images this will make performance slow and the database would grow rapidly. you can use libraries like Picasso or Glide to handle displaying bitmaps as they offer caching options and asynchronous loading. – Mark Apr 07 '16 at 14:36
  • yes saving images into database is a bad idea. and yes store it as a string reference only – dione llorera Apr 07 '16 at 14:38
  • picasso or glide is a good image loader and it caches it but it doesnt store the image for you.. you need to save it in order to retrieve the next time you need to use it – dione llorera Apr 07 '16 at 14:39
1

For my app (Memorix Notes & Checklists if you must know), I save the images itself into the app-private part of the filesystem and save the file-path in the database as a string.

Saving the images in the public filesystem can easily make your database inconsistent when a user deletes or renames the file.

If you expect many images, be aware that there is a limit on how many files are allowed per directory. In my app I make sure there are not more than 1000 images in a folder and start a new folder then.

As for the association you mentioned -- if you only need the timestamp, add a 'timestamp' field to your image table. If you need an actual 1:n relation between a 'master' object and associated images, create a 'master' table and a 'image' table and have a foreign key field in the image-table that references to the master table.

Ridcully
  • 23,362
  • 7
  • 71
  • 86
  • Good answer, with some useful additional information to take into account. – Mark Apr 07 '16 at 14:39
  • When you say "app-private", do you mean the /data/data/packagename/ folder? What would a sample filepath string look like in the database? – KaliMa Apr 07 '16 at 14:52
  • Use getFilesDir() (http://developer.android.com/reference/android/content/Context.html#getFilesDir()) to get base folder for app private file system. – Ridcully Apr 07 '16 at 15:22
1

You should be saving the path of the image into your sqlite database. Its up to you if you want the images to be in internal storage or external storage. Depends on your needs. Internal storage would always be available and would be private to your app only so user won't be able to delete the photos and you can do away with the unwanted exceptions. Visit this link to get an idea on storage options.

You'd also want to save these paths against a unique key i.e the timestamp so that you can query these images later on based on the timestamp you stored. Timestamps are always unique so you should go with it as your primary key.

You can use any library or your own implementation to lazy load these images to your UI thread.This SO thread has good ideas if you're thinking of rolling your own lazy loading.

Community
  • 1
  • 1
Parvaz Bhaskar
  • 1,367
  • 9
  • 29
  • if you want to show your images in a recycler view or a list view, if the images are too many then it'll block the UI thread ,the main thread on which your apps UI operations run. You're not allowed to do long running operations like loading bitmaps and resizing bitmaps directly onto your main thread. – Parvaz Bhaskar Apr 07 '16 at 14:53
  • Like having a list with thumbnails in it? If the list is too long / if there are too many pics, even with a RecyclerView, something will go wrong? – KaliMa Apr 07 '16 at 14:55
  • yup, only if you decide to resize it or loading it from sd-card. you'll still need a cache if you're loading just the thumbnails. You never know what kind of hardware specs phone is there on which you're app is running. – Parvaz Bhaskar Apr 07 '16 at 15:01
  • your recyclerview or listviews have adapters that run on main ui thread. – Parvaz Bhaskar Apr 07 '16 at 15:03
  • If I decide to save everything to internal storage to start off, but I want to give the user the option to back up to SD card, I assume this means copying all files/databases over to some other directory structure, so does this mean updating all the filepath strings in the copied-over db? – KaliMa Apr 07 '16 at 15:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108539/discussion-between-parvaz-bhaskar-and-kalima). – Parvaz Bhaskar Apr 07 '16 at 15:07