How should images be manages in Android when not through SQLite - android

I have some entities which I hold in SQLite DB.
Some of these entities will have image as well, as I read, SQLite DB can return up to 2MB per cursor, so storing the images in the DB isn't an option.
So I guess we are left with internal storage and self managing.
Are there any support libraries for managing these images ?
Is there a recommended design for this ?
I though about saving the images with a filename scheme of entityType_id.png where the id is the entity id in the SQLite DB, but I'm afraid it may change over time (backup / restore and such) so maybe another scheme might be a better solution ?

I used the solution that you mentioned here:
I though about saving the images with a filename scheme of entityType_id.png where the id is the entity id in the SQLite DB, but I'm afraid it may change over time (backup / restore and such) so maybe another scheme might be a better solution ?
I created a folder in my Android project and used ids that will never change. Knowing the folder and the id, which I retrieve from the DB, I am able to get the image. I used the android SQLite asset helper library for the solution.
I used such an approach for this little app: MTBcat. You can find an example here: http://www.6020peaks.com/2015/03/how-to-ship-an-android-app-with-preloaded-data/

You should store all of images on the internal storage. There is a post about that with piece of code, which could be helpful for you:
Capturing and Saving an image in Android with different names and then retrieving it by any of those names?
EDIT:
I mean external storange not internal

Have some sequence to generate unique ids (maybe just store current value as property), and store the id only in the database. Name the image file by id and store the file somewhere in the file system. If you delete the record, remove the file and do not reuse the id.
Generate own ids, independent from what the database is doing. Then they will not be affected by backup/restore or things the like.

I think storing an image which size is more than 2M in sqlite is not good for reading or whiting,maybe you can compress your image before you store it.

Related

How best to implement an Android app with large local data?

I saw a similar question but it was a decade old:
My objective is to create an app (Android/Kotlin) where the users can search/reference/lookup an individual item from a reference 'book'. Something along the lines of a dictionary or book of recipes.
The intent is for the app to be able to work offline, when the user may be anywhere.
As there could be thousands of items/records to reference it would seem to be too much for an array.
Is a Room database a good option and, if so, can a pre-loaded database be included in the package that is uploaded to Google Play?
What is best in 2022?
Yes, Room will do exactly what you want, but pre-loading it with data will be tricky (but doable).
You can't technically "pre-load" the database, but what you can do is save all the database objects to a file, using either XML, JSON, or any other format. You will then need to save that file in your project as either an Asset or Raw, and on first app launch, you'll need to read that Asset or Raw resource, parse each record, and save it to the database.
Needless to say, that initial process may take some time, so I'm not sure how good that will be from the user's perspective. But it's definitely pretty straight-forward to implement
Creating the Assets folder
Reading files from Assets

How to insert your own image into a SQLite table?

I have made a table with user info already stored in it. When the user registers with the id, all the details like username, course are retrieved from the table if the id matches. Now I want to add profile pictures in my user info table. Almost all tutorials on the internet are about saving a photo from your phone into that database and retrieving it. But i don't want that. I would like to add different image into the table for each user, i.e. it should be specific to the user. Now I know a BLOB datatype is used and the images should be converted to bytes. But I don't know where I should store the images, whether in my phone or in my res folder. Or maybe store it on the internet. I am confused.
I should mention that this isn't an ideal application, its just a model.
One of the most important factors to consider is the size of the images. If the size is likely to be in excess of say 200k then there is an increasing risk of there being issues not with storing the image in the database but retrieving the image.
Without writing your own alternative of CursorWindow then a CursorWindow (a buffer to hold a sub-group of the rows in a Cursor) is limited to 2M in size (in later versions at some it I believe it was 1M). If a picture is approaching 2MB there is no way that the picture can be retrieved (even without considering space for other columns).
As such, the generally recommended approach is to not store images but store a means of retrieving the image from a file store elsewhere (you've mentioned the res folder, which could be fine but you may need to consider the size of the APK), you could then store the file path in the database.
There's a more comprehensive answer that covers the above including storing small images in the DB and larger images elsewhere (assets folder) here
For anyone who has the same question like me, I found this tutorial to be the most easiest way to store images and retrieve it. It uses an external sqlite database to store images and info, and access it in the java page to display which i found quite easy.

Store ArrayList<CustomClass> into database Android

I have a Array list of a custom class which contain a bitmap image and a String. I want to store it in database/sharedpreferences when my app will closed and retrieve it when app is created. I already searched on google but it doesn't helped much.
Thank you for your help.
I would not recommend you storing images directly in database (I assume you are using SQLite). The cursor you get from SQLite is limited to 1MB, so if you image is in a reasonable quality, you will not get all the bytes. The best approach, in my opinion, is storing the PATH to the file in the Database, and then, in code, get that path and create a bitmap from file.

Best to store dynamic data and dynamic things in android

the scenario is that -- in my application client would make dynamic edit text (of which i m not sure ), but i want to store all the information he creates. All other part have been done.
I just want to know which is the best way to store them in File OR in Database.
As if some says in Database, i want to know how?
and if in File, why?
I will encrypt and decry pt the data too.
Any help would be appreciable.
Thanks in advance.
Store the data in a database. If you're doing Android development, simply use SQLite - http://www.sqlite.org/.
There are many reasons not to store the data on the file system:
It is easier to query a database than a file system. You can quickly and easily answer questions like "How many records do I have?" or "How many items in a given category do I have?"
If you do this in the file system, you'll have to write your own code around querying.
SQLite has strong data types that enforce a schema. If you just write to a file, you'll have to ensure an ID is an integer etc. The database can do this for you.
From my experience when writing files to the file system, I've always ended up with orphaned files. You write them and then forget they are there and they never get deleted. It's annoying. With a database you can easily assess the state of the database and remove old/unused records.

How to store data that remains after uninstall

I want to store some data that should remain also after application uninstall and to be accessible by a new version of this application.
Share preferences/files are not a solution as they are removed when program is uninstalled, also writing to internal memory is not a solution (also removed with uninstall).
Writing to external public folders I see that is not removed but this would require an external SD Card and don't want to be constrained by this.
I don't know about using the SQLite database, how it works? It could be a solution for what I want ?
Or any other solutions would be appreciated.
The databases made by your app will be stored in /data/data/your.package.name/databases/ and will be deleted on uninstallation of the app.
So, that's not a solution. I think the only way would be using the SD-card.
It sounds like you got this right. Writing to SD-card is the only really persistent way to store data.
edit:
The Data Backup might also have something going for it, but don't take my word for it ;).
Use SharedPreference or by using SQLitedatabase
1) create temporary table (with the same structure as original) and copy data from the original table into this new one 2) drop the original table 3) create the new original table (i.e. with more columns, with other column names, etc.)4) copy data back from the temporary table to this new original one 5) drop the temporary table

Categories

Resources