Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does resource id changes everytime an application starts

I am storing my images in drawable and their resource id in SQLite database.My database is created when the application starts for the first time.

Is it good to save the image ids in database or does the id's change every time an application start?

if id's change then while fetching back image id's I may receive an error.So,is storing image ID in database a good idea?

i need the images path to be stored in database with other relative data that's why i am storing the image id's in data base.

like image 666
Anirudh Sharma Avatar asked Jul 19 '14 05:07

Anirudh Sharma


People also ask

What is the use of resource ID in android?

drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R. drawable. icon ). This integer is the resource ID that you can use to retrieve your resource.

What is an application resource file?

In Android, a resource is a localized text string, bitmap, layout, or other small piece of noncode information that your app needs. At build time, all resources get compiled into your application. The resources locates inside res directory of your app.

What is resources in mobile application development?

Resources are the additional files and static content that your code uses, such as bitmaps, layout definitions, user interface strings, animation instructions, and more. You should always externalize app resources such as images and strings from your code, so that you can maintain them independently.


2 Answers

One approach would be storing the drawables in strings.xml as a string array something like this:

 <string-array name="location_flags">
    <item>@drawable/ic_image_name</item>
    <item>@drawable/ic_image_name</item>
    <item>@drawable/ic_image_name</item>
    <item>@drawable/ic_image_name</item>
    <item>@drawable/ic_image_name</item>
    <item>@drawable/ic_image_name</item>
    <item>@drawable/ic_image_name</item>
</string-array>

Then reading this array in your activity code :

TypedArray locationFlags=getResources().obtainTypedArray(R.array.location_flags);

Then applying the for loop you can get the Drawable something like this:

for(int i=0i<locationFlags.length();i++)
 {

   Drawable drawable = locationFlags.getResourceId(i, -1);
 }

Be sure to recycle the TypedArray after using it, since its a shared resource :

 locationFlags.recycle();
like image 155
nikhil.thakkar Avatar answered Sep 21 '22 22:09

nikhil.thakkar


the best way was to store image name directly into database and fetch it,then use

int resID = this.getResources().getIdentifier("your photo name fetched from database","drawable","package name");

image.setResourceID(resID);
like image 24
3 revs Avatar answered Sep 18 '22 22:09

3 revs