4

I need to save the images in my resource folder in an SQLite database. This database is pre-loaded(copied to data/ path) so the there is no populating at runtime. I've tried to save it like this in text fields in SQLite: "R.drawable.picture1".

There are about 300 small size pictures(jpg) in my drawable folder. When I run the a query ofcourse the cursor returns the particulair string. Now I need to set this as ImageResource on an ImageView, but whatever I try it doesn't seem to work.

I've tried parsing it to an int, what doesn't seem to work. Whatever I try logcat keeps telling me R.id.picture1 isn't a valid integer. I've seen a possible solution some time ago, but I've searched for it all day but can't seem to find it.

What I dont want: Convert the images to byte arrays so I can save it as a blob. I just need a way to store and retrieve an drawable reference that I can use to set the drawable on the ImageView.

I'm not that experienced so I hope someone can help me out.

Raymond P
  • 752
  • 2
  • 15
  • 27

1 Answers1

7

If you have the name of the resource you can lookup the resource identifier and use that for your ImageResource.

int resId = context.getResources().getIdentifier("picture1","drawable",context.getPackageName());
image.setImageResource(resId);;
wyoskibum
  • 1,869
  • 2
  • 23
  • 43
  • Thank you, tried that before but made the mistake of putting the whole resource.picture1 path in it. Your way did the trick. – Raymond P Jul 22 '12 at 08:26
  • I'm glad it worked for you. I made the same mistake and then realized that one of the parameters for the command was what kind of resource. – wyoskibum Jul 22 '12 at 19:34