0

I'm trying to create an app where I need to load a bunch of images, and display one after another by tapping on a simple button. But instead of manually typing every ResourceID of an image file into an array:

int imageIDs = {
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
            R.drawable.img5,
            R.drawable.img6,
            R.drawable.img7,
            R.drawable.img8,
            R.drawable.img9,
            ..
            }

I would like to just create a for loop to do the job. I'd assume this should work:

int numberOfImages = 19;
ArrayList<Integer> imageIDs = new ArrayList<Integer>();
for (int i = 0; i <= numberOfImages; i++) {
   imageIDs.add(<<resourceIDs*>>);
}
// * Don't know how to state it. R.drawable.img + i does not work.

But that doesn't seem to work. I've tried so many different approaches I found online now, sitting on it for hours, but I did not find anything that worked. Is it even possible to create an array or arrayList with the R.drawable. identifier by looping, or do I have to type it all in manually?

Is there any way to include hundreds of images within my app in Android Studio, without having to type them all in manually?

Wu Wei
  • 1,827
  • 1
  • 15
  • 27
  • Do you mean get all ids of images stored into the /drawable folder? – Jorgesys Jul 16 '18 at 23:21
  • Yes! As I understand the images are compiled in binary format into the .apk later on. They cannot be adressed by the file path they had in the Android Studio project, but only by their IDs. For example: R.drawable.img1. Now I want to create an array or arrayList that contains all IDs of all the imgs I put in the drawables folder. – Wu Wei Jul 16 '18 at 23:30

3 Answers3

0

In cases where you are using hundreds of images (i.e. games and other apps) you likely will avoid using ids for this reason. Another way to accomplish what you are trying to do is to make a Runner class that extends SurfaceView and Runnable where you override the run() method and make a few others like resume(), pause(), onTouch().

This class will be your main thread that will run your app, and instead of using ids like you are now, you will be drawing bitmaps on the screen using Canvas. An example of a game that does this is here: https://github.com/TheNamesDyl/blastar/blob/master/app/src/main/java/me/dylanburton/blastarreborn/MainActivity.java

Josh
  • 386
  • 5
  • 14
  • Thanks! That is many lines of code. I'd need a few hours to understand what's going on there. Where would I place the image files in my project and how would I load and adress them? Can you give an example? – Wu Wei Jul 16 '18 at 23:36
0

If you know the total number of images beforehand and you name them properly you can try:

List<Integer> images = new ArrayList<>();

for( int i = 0, total = 100; i < total; i++ ){

    images.add( getResources().getIdentifier("img" + i, "drawable", getPackageName() ) );

}

It's basically a reverse lookup so not as efficient, but could come in handy if we're talking about thousands of images.

user1504495
  • 666
  • 8
  • 17
  • Okay, I think that brought me close. However, I still have one error: `int numberOfImages = 19; List imageIDsList = new ArrayList<>(); for (int i = 0; i < numberOfImages; i++) { imageIDsList.add(getResources().getIdentifier("img" + i, "drawable", getPackageName())); }` It says: Identifier expected; Unexpected token; Unknown class: 'i'; Unknown class 'numberOfImages'; Unnecessary semicolon ';'. What am I doing wrong? – Wu Wei Jul 17 '18 at 11:01
  • The code you pasted works fine here. On what character is it drawing the red underline? – user1504495 Jul 17 '18 at 20:30
0

In Android Java you are trying to evaluate a string (it comes to mind ScriptEngine used in JavaX), for example :

    String sImage = "R.drawable.img" + 1;
    int image = Integer.parseInt(sImage);

and then for example load this "resource" into an ImageView.

   imageView.setImageResource(image);

This is not possible, the reason is that every resource has an int value related, and this value would change in every build.

enter image description here

you can see the value into your R.java that is generated

enter image description here

Therefore the only way to get the resources reference is creating the array of int values:

int imageIDs = {
            R.drawable.img1,
            R.drawable.img2,
            R.drawable.img3,
            R.drawable.img4,
            R.drawable.img5,
            R.drawable.img6,
            R.drawable.img7,
            R.drawable.img8,
            R.drawable.img9,
            }
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
  • 1
    Thank you for the insight! So, assuming you are right, would the right way be to store images, that are not necessarily part of the app but more of an object to be handled *by* the app, in an assets folder? Then there should be a way to load them with a loop, right? – Wu Wei Jul 17 '18 at 19:50
  • And could that also be the reason, why I'm getting the error, that I wrote in the comment on user15O4495's suggestion? Last question: Does storing the Value of the int Variable with the Identifier in a List not preserve the format of the int Value? – Wu Wei Jul 17 '18 at 19:56
  • @anarchist912 yes you could store the images inside your assets folder and use a function to read all the files: https://stackoverflow.com/questions/16234529/list-of-files-in-assets-folder-and-its-subfolders – Jorgesys Jul 17 '18 at 22:13