Kate Cebotari
Kate Cebotari

Reputation: 309

Android add multiple pictures to drawable res folder

I want to add over 200 icons (smaller than 50x50px) to the drawable resource. The drag and drop does not work for me. Using New Image asset permits to upload only a single photo.

here is my project resource folder structure:

enter image description here

After adding pictures to the drawable folder, I was unable to get the images. Here is my code

    Resources res = activity.getResources();
    String mDrawableName = user.getNat().toLowerCase() + ".png";
    int resourceId = res.getIdentifier(mDrawableName , "drawable", activity.getPackageName());
    Drawable drawable = res.getDrawable(resourceId);
    countryIcon.setImageDrawable(drawable );

Why I cannot find images in drawables?

Where should I copy the photos in the file explorer/finder? I have drawable folder and mipmap folders for different dimensions

Is it a good idea to have 200 pngs inside the project? (I have different codes like 33483477, and for every code I have a image which I need to display)

Upvotes: 1

Views: 1458

Answers (2)

Dinith Rukshan Kumara
Dinith Rukshan Kumara

Reputation: 680

I use below code to get drawables. hope it will work for you. no need of .png file extention. thats what you did wrong. Also use getResources() method before calling getIdentifier.

String mDrawableName = user.getNat().toLowerCase(); //image or icon name. not need extention .png
int resourceId = getResources().getIdentifier(mDrawableName, "drawable", getPackageName()); // or use context.getResources(). If you use fragments, use getActivity().
Drawable drawable = getResources().getDrawable(resourceId);
countryIcon.setImageDrawable(drawable );

Upvotes: 1

Jeffrey Blattman
Jeffrey Blattman

Reputation: 22647

Just copy them directly to your project under <your project>/<your module>/src/main/res/drawable[-qualifier].

Without other details of your project, I can't say specifically into which drawable resource folder these should live (-hdpi, -nodpi?).

Is it a good idea to have 200 pngs inside the project?

It depends on the size. There is an APK size limit of 100MiB. If bigger, you can stage expansion files, which are essentially opaque archives of data.

Other than that, you should consider the usage pattern of the images. Are all of the images viewed, always, by all users? Point being, if the user has to download all of them eventually, bundling them all in the APK is probably okay. If they only use a few of them, making them download them all (in your APK) is a waste of their bandwidth.

Staging the images on a server and downloading them on demand adds a little complexity to your app, but it's a well-established pattern.

Upvotes: 3

Related Questions