9

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.

Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42

3 Answers3

5

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();
nikhil.thakkar
  • 1,093
  • 7
  • 12
4

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);
Anirudh Sharma
  • 7,968
  • 13
  • 40
  • 42
0

It is Bad idea to store ids, But what is your purpose?? better idea is to store thumbnails to database rather than ids.. this helped me may this example help you do following steps

  1. get thumbnails of your image

    final int thumbnailSize = 128;// define your size

    Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);

  2. Convert it into Byte array

    Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.thumbnail); ByteArrayOutputStream out = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.PNG, 100, out); byte[] buffer=out.toByteArray();

  3. Save it as Blob

    ContentValues cv=new ContentValues(); cv.put(CHUNK, buffer); //CHUNK blob type field of your table long rawId=database.insert(TABLE, null, cv); //TABLE table name

    I havnt used this but this may help you

Community
  • 1
  • 1
jaimin
  • 563
  • 8
  • 25