Saving resources path in SQLite

帅比萌擦擦* 提交于 2019-11-30 15:55:30

问题


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.


回答1:


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);;


来源:https://stackoverflow.com/questions/11595665/saving-resources-path-in-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!