kar*_*e23 2 java android bitmap
可以从传递给R.drawable字符串的资源加载图像吗?
我正在尝试这个:
public static Bitmap LoadBitmap(Context context, String filename)
{
Bitmap image;
image = BitmapFactory.decodeResource(context.getResources(), R.drawable.filename);
return image;
}
Run Code Online (Sandbox Code Playgroud)
引发以下错误:
filename cannot be resolved or is not a field
Run Code Online (Sandbox Code Playgroud)
我正在尝试在R文件中创建一个常量字段但是抛出以下行:
R.java was modified manually! Reverting to generated version!
Run Code Online (Sandbox Code Playgroud)
我将非常感谢您的帮助或建议.谢谢
资源可以作为原始数据访问:使用AssetManager.open(..).只需传递所需位图的文件名(例如"drawable/myimage.png").
然后,您可以使用BitmapFactory.decodeStream(..)从数据流创建位图.
更新:
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;
}
Run Code Online (Sandbox Code Playgroud)