【问题标题】:Open drawable resource from string [duplicate]从字符串打开可绘制资源[重复]
【发布时间】:2013-03-25 09:25:51
【问题描述】:

我需要从字符串中获取资源:

我做什么:

InputStream is = ctx.getResources().openRawResource(R.drawable.image1);

我需要什么:

InputStream is = ctx.getResources().openRawResource(getDrawable("image1"));

有人知道如何实现getDrawable()函数吗?

谢谢!

【问题讨论】:

    标签: android


    【解决方案1】:

    使用getResources().getIdentifier 获取可绘制ID,使用字符串名称作为:

    int drwableid = ctx.getResources().getIdentifier("image1", 
                                             "drawable", ctx.getPackageName());
    InputStream is = ctx.getResources().openRawResource(drwableid);
    

    【讨论】:

    • 谢谢!!完美运行!
    猜你喜欢
    • 1970-01-01
    • 2017-04-16
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    相关资源
    最近更新 更多