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.
Related
I need to do an Quiz App with pictures associated to the questions.
The problem is that there should be default questions with the app and in future the app shall be able to create more questions and i dont know how to store the pictures paths on database mainly because of the default questions.
My first aproach is to have the default pictures in the res/drawables but how can i get the path of res/drawables pictures?
I hope to have explained my problem well.
Res/drawables doesn't have paths. They have integer ids. So there's no path to get. Those ids also change every compile, so hardcoding them isn't always ideal.
I would suggest that the first time you start up, you write those default images to disk. This gives you two benefits- one you have a path. Two, you can treat default images and non-default images with the exact same code.
Have a look on Android Save and Retrieve Image Path From Database
I am developing an E-commerce application. Here I need to show lots of product images when the app is up. I have stored few product images in the drawable folder to reduce the network usage. I am planning to download delta product images when the new product is added in my back end product inventory and save it in SQLite database.
In the app I have to refer two different path ( drawable and SQLite database) to get all product images.
So how can I move the initial fish images in drawable folder to SQLite database at the time of APK generation OR after the app installation (in onCreate method of my MainActivity). And I also need to delete those initial images in the drawable folder after moving it to SQLite database.
Any suggestions are appreciated. Thank you in advance.
The bottom line is that if you deliver with content bound into your APK, it will remain there. Assuming you're planning on distributing through the Google Play store, the only way to modify this via an app update, which is obviously extremely hacky. So if your goal is to discard initial drawable resources, you cannot do this.
My advice is to compress the images as much as possible and only provide one image size which you can resize at runtime, so use the nodpi drawable resource folder (i.e. drawable-nodpi)
The alternative is to download the images via network. Though you say you don't want to do that the file sizes can be made quite small and so the download fast in most cases, so unless you're sure network connectivity will be a problem for your users it's a viable option.
I want to store array of images for different records.There is a list view for records and when user clicks on particular record new activity is started and i want to display the array images there. How can i do it?
I tried storing images into draw able folder and using them with R.id but it makes my app heavy.
there are several way to dealing with it but not solve it!
1: you can store your image in your assets folder. it can help you to manipulate and working with your images.
2: reduce your image resolutions.
3: create a web server and request your image from the server instead of save them in your local storage.
also these are not solution but some good trick to deal with this problem!
I want to create an android application containing the following data:
Text in four different languages, a date and an image.
My problem is that I don't know the best way to store those data. They should be available for the lambda user but not easily visible (--> not on the sd card for example). Without image, I would directly have chosen sqlite but... with it, whats the best?
assets? sqlite(+ image as blob)? sqlite(+ path to image)? XML? JSON?
Also, I want to randomly pick one of my about 1000 entries and thos entries will probably be inserted by someone who has low computer science knowledges.
The files that are stored inside your application folder are private to your application and cannot be easily accessed.
So easiest would be to have the text as xml and images as assets inside your app folder.
If the images are static (ie.. not downloaded but distributed with your app) I would put these in assets (drawable).
With regards to the text, you could use strings.xml and localize this for the other 3 languages.
Hope I have understood your problem here.
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