I've searched Google and this website many times, but can not find a solution to my question.
First, I understand, you can load images using something like:
int image = R.drawable.icon; (assuming there's a file called 'icon.png')
and I've read about, and tried using:
getResources().getIdentifier("resname", "restype", com.example.whatever);
But, here's what I'm unable to do: I want to be able to have pictures in the /res/drawable folder (I believe that's the correct folder), WITHOUT KNOWING ANY OF THE NAMES OF THE FILES, and load them dynamically - at run-time.
One (of the many) things I've tried is (something like):
int[] images = new int[numberOfImages];
for( 0 to numberOfImages )
{
images[i] =
getResources().getIdentifier("resname", "restype", com.example.whatever);
}
This returns 0 EVERY TIME, so it's not going to work
I'd like to get the name and integer identifier for every picture in the /res/drawables folder. Can this be done WITHOUT KNOWING ANY FILE NAMES?
[adding this after the question was resolved to help anyone that may run into the same issue in the future. It just shows that it does in fact work.]
Class resources = R.drawable.class;
Field[] fields = resources.getFields();
String[] imageName = new String[fields.length];
int index = 0;
for( Field field : fields )
{
imageName[index] = field.getName();
index++;
}
int result =
getResources().getIdentifier(imageName[10], "drawable", "com.example.name");
public class DrawableManager { private static Context context = null; public static void init(Context c) { context = c; } public static Drawable getDrawable(String name) { return R. drawable.? } }
Supported file types are PNG (preferred), JPG (acceptable), and GIF (discouraged).
Step 1: In this method first of all in your system find your required images and copy the image as we do normally. Step 2: Then open the Android Studio go to the app > res > drawable > right-click > Paste as shown in the below figure. Step 3: Then a pop-up screen will arise like below.
Drag and drop your images directly onto the Resource Manager window in Android Studio. Alternatively, you can click the plus icon (+), choose Import Drawables, as shown in figure 3, and then select the files and folders that you want to import. Figure 3: Select Import Drawables from the dropdown menu.
Well, if you have to do it, use reflection :)
Class resources = R.drawable.class; Field[] fields = resources.getFields(); for (Field field : fields) { System.out.println(field.getName()); }
However, in a way, you are still hard-coding stuff, since you will only be putting a fixed set of drawables in your app. :)
As you stated in the comments, if you want to add files to that folder after the app is installed you'll have to use a private folder. Data storage documentation.
Then use a service to download images and add them to the private folder.
If you want the user to download the app with some preloaded images and you don't want to wait for the service run for the time to download the initial set of images, you can always send some images in the /res/raw
folder and in the first time your app runs, copy these to your private folder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With