0

I want to read all the Images in drawable folder as File object and store them in an array of File. Is there a way to get list of all images in drawable and read them as File?

Edit: I need to know the following..

  1. How to collect all the images from drawable folder. My images have different meaningful name based on the content. There is no common pattern in the name.

  2. How to read these images as File.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
Harshita Sethi
  • 2,035
  • 3
  • 24
  • 46

2 Answers2

1

No, because they are not files on the device. They are only files on your development machine. Resources are entries in the APK itself at runtime.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

First you need to convert all drawable to bitmap and save to Local Storage then read it as File

You can get all Drawables from this method

public Drawable[] getAllDrawables()
    {

        Field[] ID_Fields = R.drawable.class.getFields();
        Drawable[] drawables = new Drawable[ID_Fields.length];
        for(int i = 0; i < ID_Fields.length; i++) {
            try {
                drawables[i] = ContextCompat.getDrawable(this,ID_Fields[i].getInt(null));
            } catch (IllegalArgumentException | IllegalAccessException ignored) {
                return null;
            }
        }
        return drawables;
    }

Possible Duplicate

Here

Community
  • 1
  • 1
Sabish.M
  • 2,022
  • 16
  • 34
  • Instead of Bitmap can it be converted to Map, and again half of the question stands how to get a collection of all the files from drawable folder. – Harshita Sethi Aug 23 '16 at 12:40
  • With the above code I see many unwanted files in drawable folder. From my side I added 8 images but here the length of the array is coming as 85. I checked the drawable folder in my disk for any hidden files also but there were none. Can you help in extracting only those images files what I added there? – Harshita Sethi Aug 23 '16 at 17:19
  • because when build time android generate default themed drawables and add it to apk so the array size is increased this cause you need to add the unique id of drawable names and filtter from array with unique id. – Sabish.M Aug 24 '16 at 07:08