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.
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.
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.
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.
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();
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With