Android 将ImageView源设置为来自SQLite的图像

Android 将ImageView源设置为来自SQLite的图像,android,image,sqlite,imageview,android-imageview,Android,Image,Sqlite,Imageview,Android Imageview,我在我的项目中有一个小的SQLite数据库,其中有表“Products”。此表的一列是字符串“image”。在活动中,我在Intent extras中接收一个产品id,并从DB中创建一个产品对象。在这个活动的布局中,我有一个ImageView。每次创建产品对象时,我都要将ImageView图像设置为存储在对象中的图像。我试着做到以下几点: ImageView product_image = (ImageView) findViewById(R.id.product_image); product

我在我的项目中有一个小的SQLite数据库,其中有表“Products”。此表的一列是字符串“image”。在活动中,我在Intent extras中接收一个产品id,并从DB中创建一个产品对象。在这个活动的布局中,我有一个ImageView。每次创建产品对象时,我都要将ImageView图像设置为存储在对象中的图像。我试着做到以下几点:

ImageView product_image = (ImageView) findViewById(R.id.product_image);
product_image.setImageDrawable(Integer.parseInt("R.drawable."+productToPresent.getImage());
productToPresent
是我的产品对象,getImage返回图像字符串名称。 但是,此时我的应用程序崩溃,我在logcat中收到以下错误消息:

RuntimeException:无法启动活动组件信息{com.myapp.appname/com.myapp.appname.ActivityProductDetails}:java.lang.NumberFormatException:无效int:“R.drawable.cat02_prod02”
其中cat02_prod02是存储在my DB中的字符串和图像的文件名


那么如何才能做到呢?

您可以通过以下方式做到:

int id = getResources()
       .getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);
这将返回要访问的绘图表的
id
。。。然后,您可以通过执行以下操作在
imageview
中设置图像

imageview.setImageResource(id);

是的,效果很好!今天学到了一些新东西。非常感谢你!