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.
My code is basically
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.
}
but imgID being returned is always 0.
I also tried another approach
String imgName = "android.resource://" + this.getPackageName() + "/drawable/image_" + i;
b = BitmapFactory.decodeFile(imgName);
if (b != null)
//etc
But b is always null.
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!
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