Issue
I have a folder structure inside my various drawable folders, e.g. drawable, drawable-hdpi etc.
Something like:
drawable-hdpi
├── folder_main
│ ├── sub_folder_1
│ │ ├── 1.png
│ │ └── 2.png
│ ├── sub_folder_2
│ │ ├── 1.png
│ │ └── 2.png
│ └── sub_folder_3
│ ├── 1.png
│ └── 2.png
└── ic_launcher.png
Accessing these resources will be dynamic and done programatically. I need a way to access these sub folder png resources.
Accessing the ic_launcher
is straight forward using: R.drawable.ic_launcher
. Logically I think you should be able to access the resource somthing like:
R.drawable.folder_main.sub_folder_1.1
But that clearly doesn't work and wouldn't work programatically where the sub folder and file is dynamic.
Thanks in advance for any pointers.
Solution
Shortly after posting my question I found this stack overflow resource:
http://www.stackoverflow.com/a/10170925/321697
This, and the answers here solved my first problem to do with folder/file structure.
Then I needed a way to process these resources. I did this as follows:
I updated the file structure to:
drawable-hdpi
->name_1_0.png
->name_1_1.png
->name_1_2.png
->name_2_0.png
etc
I then accessed the files with the following code:
int folder = 1;
int image = 0;
int resourceIdentifier = resources.getIdentifier("name_"+folder+"_"+image,"drawable","*whateveryourpackageiscalled");
Note: *whateveryourpackageiscalled = com.yourwebsite.etc
This way I get to dynamically access the image I need by altering the folder
and image
variables.
Answered By - HGPB
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.