可以从传递 String 参数的资源中加载 R.drawable 吗?

发布于 2024-10-20 16:49:02 字数 517 浏览 1 评论 0原文

可以从传递给 R.drawable 字符串的资源中加载图像吗?

我正在尝试这样做:

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

抛出以下错误:

filename cannot be resolved or is not a field

我正在尝试在 R 文件中创建字段,创建一个常量,但抛出以下行:

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

我将感谢您的帮助或建议。谢谢

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

I'm trying this:

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

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!

I'll appreciate your help or suggestions. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情魔剑神 2024-10-27 16:49:02

资源可以作为原始数据访问:使用 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;
}

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

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

Update:

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;
}
找回味觉 2024-10-27 16:49:02

@Peter Knego(没有足够的声誉来直接注释答案 - 愚蠢的SO)

我对资产管理器的(有限)理解是它允许访问 foo/assets 目录中的文件。因此 Peter 的代码将访问:

foo/assets/drawable/myimage.png

我认为 @karse23 想知道他是否可以访问 res/drawable 目录中的图像:

foo/res/drawable/myimage.png

为什么要这样做?好吧,如果您想从应用程序和第三方应用程序访问资源。理想情况下,您可以使用拥有资源的应用程序中的 R(这样您就可以在布局中修改该 id)并在第 3 方应用程序中使用名称(您无权访问 R 类)。

我正在尝试执行此操作,Peter 的方法是在我的代码中引用资产目录(但我再次访问另一个包中的资产)。 (您可以通过记录 assetManager.list("") 返回的字符串来检查这一点)。

为了支持 Peter,文档说:

public final AssetManager getAssets ()
Retrieve underlying AssetManager storage for these resources.

这似乎确实支持 Peter 建议的行为(也许是我正在使用的旧 Android 中的错误???)。

总之,我认为解决方案是破解资产目录中的文件并通过代码在父应用程序中访问它们:((或者按照 android 家伙使用 ContentProvider 的方式进行操作)。

@Peter Knego (not enough reputation to annotate the answer directly - stupid SO)

My (limited) understanding of the asset manager is that it allows access to files in the foo/assets dir. So the code from Peter would access:

foo/assets/drawable/myimage.png

I think @karse23 wants to know whether he can access images in the res/drawable dir:

foo/res/drawable/myimage.png

Why would you want to do this? Well if you want to access resources from an app and also 3rd-party apps. Ideally you do it using R from the app which owns the resource (so you can whack that ids in the layouts) and by name in the 3rd-party app (where you don't have access to the R class).

I'm trying to do just this and Peter's approach is referencing the assets dir in my code (but then again I'm accessing the assets in another package). (you can check this by logging the strings returned by the assetManager.list("")).

In support of Peter the doc says:

public final AssetManager getAssets ()
Retrieve underlying AssetManager storage for these resources.

which does seem to support the behaviour Peter is suggesting (maybe a bug in the old Android I'm using???).

In conclusion I think the solution is to whack the files in the assets dir and access them in the parent app from code :( (or do it the way the android dudes intended using a ContentProvider).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文