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
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
get thumbnails of your image
final int thumbnailSize = 128;// define your size
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);
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();
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
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);
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();