gpt4 book ai didi

Android(最小 2.1): load images from name string

转载 作者:行者123 更新时间:2023-11-29 22:11:17 25 4
gpt4 key购买 nike

我希望能够从 res/drawable 动态加载图像。我不知道会有多少文件,但我知道它们将按照惯例命名为“image_N”,其中 N 是连续整数。应用程序运行时可能会上传新图像。

我的代码基本上是

Resources rs = getResources();
String imgName = "android.resource://" + this.getPackageName() + "/drawable/image_" + i;
int imgID = rs.getIdentifier(imgName , null, getPackageName());

if (imgID != 0)
{
Drawable d = rs.getDrawable(imgID);
//etc.
}

但返回的 imgID 始终是 0

我也试过另一种方法

String imgName = "android.resource://" + this.getPackageName() + "/drawable/image_" + i;
b = BitmapFactory.decodeFile(imgName);
if (b != null)
//etc

但是b总是null

我知道文件夹中有名称正确的文件,因为我在那里放了一些。不知道我做错了什么。任何帮助将不胜感激!

最佳答案

我相信您正在使用 getIdentifier(...)不正确。这应该有效:

int id = Context.getResources().getIdentifier("image_" + i, "drawable", getPackageName());

这相当于一个 id R.drawable.image_<+i>

关于Android(最小 2.1): load images from name string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9575746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com