I'm just looking for some insight into what would be the best way for me to store images as part of my app.
I have an activity that represents a 'Job' which has a couple of edittext's and underneath was planning on using the Gallery component to show images relevant to this job.
The job data is stored in a database (on the sdcard) so was also thinking of creating a table to store 'JobImages' and having each image stored as a byte array.
But I'm not sure if it would be better to store the images directly on sdcard under a folder structure specific to my application and the job. E.g. using the job ID number as a folder name.
Depending on which method I use will greatly determine the code that goes into an 'adapter' that allows me to bind to the gallery component so before I begin I was wondering if anyone has had the same design problem and what option they chose.
Thanks,
Dave
Regardless of what storage method you choose, don't let that stop you from writing the code that will use it. Write a class that abstracts this from your app and just gives you images, how/where it retrieves images from, doesn't matter, this will also help you in the future if you decide to change your storage method, you will only have to change this class, not the whole app.
Back to the original question, it depends how you'll be using the images, if you already have a db and need to associate the images with other records or add additional properties (i.e. a database of animals in a shelter with their pictures and other attributes), makes sense to store in a db. If all you care about are pictures that don't have any need to be organized (i.e. the built in Gallery), then store in a folder.
Here's a link on how to store in DB: http://www.helloandroid.com/tutorials/store-imagesfiles-database
Related
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
I am developing a notepad app which can store simple text files and checklists. Currently I maintain a separate file (say info.txt) that maintains information about whether a given file is a simple text file or a checklist and based on that I render my UI (for either listing all files or opening a file) to show that file in my app. However I am not very happy with this approach because is slow and does not appear to scale well.
Is there a better way to add "metadata" (e.g. if it is a simple text or cheklist, tags, etc) about a file in android?
Any help will be greatly appreciated
There are several ways of storing persistent data in Android.
The way you are currently doing it is through the device storage, and you are quite right it would probably not scale well in addition to being directly accessible to the user meaning they could edit or delete your metadata.
Using SharedPreferences would be one way of storing the metadata which has the advantage of being completely hidden from the user, as well as being relatively easy to set up. The main disadvantages I can see are that it may not scale well if a user has a large number of files, and it is much more difficult to retrieve files with certain criteria, a certain tag for instance, as you mention in the comments.
The best way to store data that will scale well, be persistent, and let you run queries on the data would be an on device SQLite database. SQLite will usually have more overhead in terms of setup time, but is far more robust and featured than any of the other options besides perhaps network based storage, which based on the information you have given is probably not something you are interested in. Based on your problem the SQLite database is probably the way to go and has the bonus of being expandable in case you ever decide to add more information, or even store the files in the SQLite database.
I am writing an app that contains a list of items with their information. When the users open the app, they would see the list, and when they select a particular item, they would get all the information about that item.
Right now my approach is I store the data in a multidimensional array in the java source file. When I push a new update, I might add new items in the java source file (so the array gets bigger). I wonder if this is the best approach. I tried looking up relevant information about array and database on the Internet, but I can't seem to find the information I need. Any advice for me?
Also, if in the future, I create a function for users to add their own items to the list, what's the impact?
If the user should be able to update it, if you should be able to update it dynamically (for instance update from internet), then the database is a must.
If that data is static and won't change unless you update the app, you can store it in the code or better, in a file (you can store in JSON format for ease of reading & parsing)
If use array, the newly added items by the user will be gone when the app restarts.
You can use a SQLite database stored directly on your phone (sd card for example).
An analysis of the storage options and what they're commonly used for can be found here. Personally I suggest SQLite database.
The database is good for permanent data, but realize it can create a bottleneck if writing to disk is not really necessary.
I am in the middle of refactoring an App to no longer use SQLite. I was inserting large amounts(10,000-100,000) of new rows at once, and it looked like it was going to take an hour based on my log feedback of the status. When I keep it in memory, it all loaded in about 5 seconds.
fellow programmers! I'm new... hope you can help!
So. I'm currently developing an application that allows users to choose four different images from a set of images that I have saved in my res/drawable/ folder, and then save those choices as an entry in a database.However, I then need to be able to redisplay those four images when the user asks for them, and I'm not sure what the best way of storing the images is since my current setup isn't going to work for me in the future.
Right now, I have it working so that I actually insert the R.java int identifiers into the database for each of the images, so the database has four columns: img1(int), img2(int), img3 (int), img4 (int). Then when I write a query, I just use those values in place of R.drawable.someImage.
It works fine. Except... when I add new images to my res/drawable folder, all of the drawable ids get changed! Then the ids I have in the database are wrong, and some pull up the wrong images, and some throw NullPointerExceptions!
This is a problem, because I want to be able to update the app with new images after the users download it. How should I be storing these images so that they can be dynamically chosen, but then be reliably be referenced again?
This is a problem, because I want to be able to update the app with new images after the users download it.
I don't quite understand this - if you are going to distribute new images 'after' the users download your app then the new images are never going to have resource ids (as found in R.java) as these are auto-generated as part of the build process.
Do you really mean this or do you intend to update the app with new images and have the users download it again?
If you are going to update the app with new images periodically and have the users download the updated app, you can find the resource id at runtime using...
int resId = getResources.getIdentifier("img1", "drawable", "com.mycompany.mypackagename");
At this point, of course, you'll need to know the names of all the drawables although this could be achieved with a string array in your res/values/strings.xml.
If, however, you want to add new images without the need for the user updating the app, I'd think about maintaining an image directory (on the SD card for example). This way, you would simply just need to store the path to the image files in the DB.
Resources can be accessed as raw data: use AssetManager.open(..) Then you can use BitmapFactory.decodeStream(..) to create a Bitmap from the data stream.
So you can just save filenames of images and instantiate Bitmaps via above commands.
I have another question also, one I haven't yet got round to having a bash at coding yet so feel free to complain and tell me to try it first. But I just wanted to know if there is a way of storing data such as references to images
i.e I would have a customer object with a name, age etc and then an image which is contained within the drawable class. So when starting up the Activity (after the first time) I could obtain the name/age primitive types easy enough off of the data store but then when it comes to the image if I store it's id how would I then use that as a reference for the bitmap loader. To obviously avoid retaining two images (the one supplied with the app and if I had to store one to keep the association with the customer object).
Unless the users of your app has a common and amazingly static set of customers you probably don't want to distribute customer images bundled as resources in your app! Store them in the database as blobs.