的Android(2.1分):从名称字符串载入图像 [英] Android (min 2.1): load images from name string
问题描述
我希望能够从RES /绘制动态加载图像。我不知道有多少文件会有,但我知道他们将被命名约定为image_N,其中N是连续的整数。这是可能的新图像将应用程序运行时进行上传。
I want to be able to load images dynamically from res/drawable. I don't how many files there will be, but I do know they will be named by convention as "image_N", where N is sequential integer. It is possible that new images will be uploaded while the app is running.
我的code是基本
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
总是空
。
我知道有该文件夹中的文件与正确的名字,因为我放了几个在那里。不知道我在做什么错。任何帮助将大大AP preciated!
I know there are files in the folder with the right names because I put a few there. Not sure what I'm doing wrong. Any help would be greatly appreciated!
推荐答案
我想你搞错编译资源,里面的文件夹的文件。在Android中,资源是静态的,据我所知。一旦编译,你无法通过内部绘制文件夹中文件拖放添加新的资源,就没有标识的新文件。 您应该使用一个文件夹中的数据或更好的要求Android的API与getFilesDir()或类似的东西。
I think you've mistaken compiled resources with files inside folders. In Android, resources are static as far as I know. Once compiled you can't add new resources by dropping files inside "drawable" folder, there would be no IDs for new files. You should use a folder in DATA or better ask Android API with getFilesDir() or something similar.
有关更多信息,请查看>> 这里一>
For more info, take a look >>here
这篇关于的Android(2.1分):从名称字符串载入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!