0

I want to change the image resource for bitmap dynamically. But I can't call them with name from the drawable class.

bmp = BitmapFactory.decodeResource(getResources(), R.drawable.getImage(foldername + "/" + imagename));

I need to do something like that but I couldn't find the proper way of doing this.,

EDITED

I think I should be more clear. I store my images under the drawable folder and they are separeted into other folders.

For example;

drawable/imageset1, drawable/imageset2,

and I want to change the Image resource for bitmap depending on user input.

For example: User selects imageset5 from first spinner, and selects image5.png from another spinner.

Reno
  • 33,594
  • 11
  • 89
  • 102
Srht
  • 473
  • 3
  • 7
  • 17
  • 2
    check http://stackoverflow.com/questions/11273828/dynamic-naming-of-resources-in-android-without-getidentifier/11273920#11273920 – Khan Jul 06 '12 at 10:28
  • @Srht, I have prepared demo, which is working. It something like duplicate code, but if you want it, then just comment here. – Chintan Raghwani Jul 06 '12 at 10:55
  • @ChintanRaghwani It doesn't worked for me. Yes I would like to have the working code. – Srht Jul 06 '12 at 10:59
  • I works when you store images under drawable folder. But It does not work for folder hierarchy. I think Android does not support folders under drawable. – Srht Jul 06 '12 at 11:03
  • I have added and updated answer, just check it. – Chintan Raghwani Jul 06 '12 at 11:09
  • can't you see http://stackoverflow.com/questions/1077357/can-the-android-drawable-directory-contain-subdirectories in my answer which clearly says that you cannot access images from sub-folder created in your drawable folder so are you still doubtful? – Umar Qureshi Jul 06 '12 at 11:28
  • @Sam.Janz I have seen it but it would be better if I could use folder hierarchies. So I'm trying to find a way around. Creating any folder under res folder is also forbidden? – Srht Jul 06 '12 at 11:32
  • you can try like category1image1 and then instead passing full name of image just pass the index of category and the image to get resource it would be a better approach – Umar Qureshi Jul 06 '12 at 12:35

5 Answers5

2

I hope this will do what you want

BitmapFactory.decodeResource(getResources(), getResources().getIdentifier(foldername + "/" + imagename , "drawable", getPackageName());

EDITED:

while the above code will work only if you follow android rule which doesn't allow sub directories in drawable folder so the above code will work only when directly accessing images from drawable.

BitmapFactory.decodeResource(getResources(), getResources().getIdentifier( imagename , "drawable", getPackageName());

As these links describe

How to access res/drawable/"folder"

Can the Android drawable directory contain subdirectories?

Community
  • 1
  • 1
Umar Qureshi
  • 5,985
  • 2
  • 30
  • 40
0

Make one array of image resources

int []a = int[]
{
   R.drawable.one,
   R.drawable.two,
   R.drawable.three,
   R.drawable.four,
};

Then access it in loop

for(int i=0;i<a.length;i++)
{
   //  a[i]
}
MAC
  • 15,799
  • 8
  • 54
  • 95
0

Is it a resource stored in the drawables folder? Then try this method from BitmapFactory

public static Bitmap decodeResource (Resources res, int id)

Where

res = getResources()

and id is the id of the drawable like

R.drawable.picture

Check it out here

Soham
  • 4,940
  • 3
  • 31
  • 48
0

you can't do like this. You have to add all images in drawable & use it like

myImage.setBackgroundResource(R.drawable.imageName)

or after downloading the image from web you can apply as

myImage.setBitmap(BitmapFactory.decodeStream(in));

to get the bitmap where in is InputStream

Soumyadip Das
  • 1,781
  • 3
  • 16
  • 34
0

Just try following: Your image having name "imagename" must be in the folder

String IMAGE_FOLDER_NAME = "YOUR_IMAGE_CONTAINING_FOLDER";

Now, use following code:

String imagename = "YOUR_IMAGE_NAME";    
String PACKAGE_NAME=getApplicationContext().getPackageName();    
int imgId = getResources().getIdentifier(PACKAGE_NAME+":"+IMAGE_FOLDER_NAME+"/"+imagename , null, null);
System.out.println("IMG ID :: "+imgId);    //    check this in log
System.out.println("PACKAGE_NAME :: "+PACKAGE_NAME);    //    check this in log
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imgId);
Khan
  • 7,585
  • 3
  • 27
  • 44
Chintan Raghwani
  • 3,370
  • 4
  • 22
  • 33