1

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!

Sabrina S
  • 337
  • 8
  • 21

4 Answers4

2

I believe you are using getIdentifier(...) incorrectly. This should work:

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

which would is equivilent for an id R.drawable.image_<+i>

triad
  • 20,407
  • 13
  • 45
  • 50
0

in Adapter i use this

   Context context;
   public MyAdapter0(Context context, 
   List<String>   moduleList,List<String> moduleList2) {
   mInflater = LayoutInflater.from(context);
   for (int i=0;i<moduleList.size();i++) {
   int id = context.getResources().getIdentifier("color6_"+i,
   "drawable", context.getPackageName());
    mItems.add(new Item(moduleList.get(i),moduleList2.get(i),id));
   // Log.d("idimage", String.valueOf(R.drawable.color6));
    }
sirmagid
  • 1,090
  • 1
  • 16
  • 23
0

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

ruhalde
  • 3,521
  • 3
  • 24
  • 28
  • Ah! Thank you. If that's true, it makes things simultaneously easier and more difficult for me. :) The default images that I put in my res/drawable I should just be able to refer to directly (perhaps via a foreach statement?) while the others I will need to go through the file resources - or perhaps the load through URL API. – Sabrina S Mar 06 '12 at 01:48
  • For the default list of images in R.drawable, I found that part of the solution (here)[http://stackoverflow.com/a/3950277/1181368]. – Sabrina S Mar 06 '12 at 03:33
0

I use this:

String drawableName = "drawableNameAsDeclaredInDrawableDir" // in your case "image_" + i;
int drawableResId = resources.getIdentifier(packageName + ":drawable/" + drawableName, null, null);

Then use

Drawable d = resources.getDrawable(drawableResId);
YuviDroid
  • 1,546
  • 11
  • 12
  • Btw as ruhalde pointed out you cannot add drawable resources at runtime. You will have to load new images on the sd card or internal memory. – YuviDroid Mar 06 '12 at 00:08
  • Hi YuviDroid - thanks! But your code is basically identical to mine, no? Except it looks like you leave out `"android:resource//"`. – Sabrina S Mar 06 '12 at 01:44