这可以从传递一个字符串参数加载资源R.drawable? [英] It's possible to load R.drawable from resources passing a String parameter?

查看:834
本文介绍了这可以从传递一个字符串参数加载资源R.drawable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可能的,从传递到R.drawable一个字符串资源加载图像?

It's possible to load an image from resources passing to R.drawable a String?

我想这样的:

public static Bitmap LoadBitmap(Context context, String filename)
{
     Bitmap image;
     image = BitmapFactory.decodeResource(context.getResources(), R.drawable.filename);
     return image;
}

抛出了以下错误:

Throws the following error:

filename cannot be resolved or is not a field

我试图创建R档creatic恒定的场,而抛出以下行:

I'm trying to create the field in the R file creatic a constant but throws the following line:

R.java was modified manually! Reverting to generated version!

我将AP preciate你的帮助或建议。谢谢

I'll appreciate your help or suggestions. Thanks

推荐答案

资源可以访问原始数据:使用<一个href=\"http://developer.android.com/reference/android/content/res/AssetManager.html#open(java.lang.String)\"相对=nofollow> AssetManager.open(..)。只是通过通缉位图的文件名(例如,绘/ myimage.png)。

Resources can be accessed as raw data: use AssetManager.open(..). Just pass the filename of wanted bitmap (e.g. "drawable/myimage.png").

然后你可以使用<一个href=\"http://developer.android.com/reference/android/graphics/BitmapFactory.html#de$c$cStream(java.io.InputStream)\"相对=nofollow> BitmapFactory.de codeStream(..)创建从数据流中的位图。

Then you can use BitmapFactory.decodeStream(..) to create a Bitmap from the data stream.

更新:

public static Bitmap LoadBitmap(Context context, String filename){
    AssetManager assets = context.getResources().getAssets();
    InputStream buf = new BufferedInputStream((assets.open("drawable/myimage.png")));
    Bitmap bitmap = BitmapFactory.decodeStream(buf);
    // Drawable d = new BitmapDrawable(bitmap);
    return bitmap;
}

这篇关于这可以从传递一个字符串参数加载资源R.drawable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆