0

i Have more than 1000 images in my drawable folder, and i need to load all these images in an array-list in order to search later when the program running. Is there any possible way in android that i can load all the images in an ArrayList dynamically?

thanks

7
  • 3
    with 1000 images, did you thought about APK size? Commented Sep 5, 2018 at 14:27
  • 1
    Yes, you can but you shouldn't. Commented Sep 5, 2018 at 14:28
  • 2
    wow. yeah i wouldnt store those in a arraylist. i wouldnt store them locally either. why? you should look into storing them in a server and then saving a thumbnail with it so it loads faster if you are concerned about that. at that point, you can call the server, get the images, and then store them in a list to parse thorugh. but still, it will still take a long time. you need to rethink your approach here. Commented Sep 5, 2018 at 14:29
  • I hope you're planning to fill up the ArrayList with image paths or references, not with the binaries. Commented Sep 5, 2018 at 14:33
  • The answer below is the correct answer but I believe you have a bigger problem. My suggestion to your 1000 images problem is to use simple cloud services(Dropbox, Firebase Storage...) to store the images and an Image processing library to load them (Picasso, Glide...) via their URL. Commented Sep 5, 2018 at 14:46

3 Answers 3

3

This code is working and tested by me, you can get all images from drawable and save it in an ArrayList.

But in my opinion that is a very very bad idea

Code:

 ArrayList<Integer> arrayOfIcons = new ArrayList<>();

Field[] drawables = R.drawable.class.getFields();
for (Field f : drawables) {
    try {
        int resID = getResources().getIdentifier(f.getName() , "drawable", getPackageName());
        arrayOfIcons.add(resID);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

ImageView imageView = findViewById(R.id.imageView);
imageView.setImageResource(arrayOfIcons.get(0));

I hope it helps you.

Sign up to request clarification or add additional context in comments.

Comments

0

import java.lang.reflect.Field;

Field[] ID_Fields = R.drawable.class.getFields();
int[] resArray = new int[ID_Fields.length];
for(int i = 0; i < ID_Fields.length; i++) {
    try {
        resArray[i] = ID_Fields[i].getInt(null);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Comments

0

This is the final working code:

 ImageView imageView = findViewById(R.id.testImage);
    //load all the icon names from drawable
    ArrayList<String> arrayOfIcons = new ArrayList<>();

    //load the array containing specific characters
    containingArray = new ArrayList<>();

    Field[] drawables = R.drawable.class.getFields();
    for (Field f : drawables) {
        try {
            int resID = getResources().getIdentifier(f.getName() , "drawable", getPackageName());
            arrayOfIcons.add(f.getName());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //search for all similar keywords
    for(int i =0; i < arrayOfIcons.size();i++){
        if(arrayOfIcons.get(i).contains(title_string) && arrayOfIcons.get(i).contains("_")){
            containingArray.add(arrayOfIcons.get(i));
        }
    }
    //split array
    for(int i = 0; i < containingArray.size();i++){

        //finding the index that contains the keyword 
        if(containingArray.get(i).substring(0,containingArray.get(i).lastIndexOf("_")).equalsIgnoreCase(title_string)){
            Context context = imageView.getContext();
            int id = context.getResources().getIdentifier(containingArray.get(i), "drawable", context.getPackageName());
            imageView.setImageResource(id);
        }
        System.out.println("ContainingArray: "+containingArray.get(i));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.