0

I am displaying almost 4,000 images in a loop with image name.

Here is the code which i am using to get my images from drawable folder

     for(int i=0; i<count(images_array); i++) {
            mDrawableName = images_array(i);

            int resID = res.getIdentifier(mDrawableName, "drawable", activity.getPackageName());
            Drawable drawable = res.getDrawable(resID);
            image.setImageDrawable(drawable);
     }

the issues are:

  1. When the image name is not found in resource folder my app throws me an exception and crashes.
  2. Is there any better way to load 4000 images from drawable in listview? Is there any way i can check if image is not in drawable then show placeholder image ?
spazm
  • 3,545
  • 26
  • 29
  • I don't see a problem here ... you get an exception because the resource doesn't exists. Catch the exception and do what you want in this case. A better way ... not loading 4000images ? Unless they do 10pixels, you won't be able to show all of them in once, so try to load only what you need. – AxelH Oct 21 '16 at 06:53

2 Answers2

1

When the image name is not found in resource folder my app through me an exception and crash.

This is not an issue since it is expected behavior that getIdentifier returns 0 for a non-existent resource and then getDrawable throws the Resources.NotFoundException for id = 0 (which is not a valid ID).

Is there any way i can check if image is mot in drawable then show placeholder image?

You either catch that exception or check if getIdentifier returned 0.
I don't know the rest of your code, so based on what you posted, you could do this:

for (int i=0; i<count(images_array); i++) {
    mDrawableName = images_array(i);

    int resID = res.getIdentifier(mDrawableName, "drawable", activity.getPackageName());
    Drawable drawable;
    if (resID == 0) {
        drawable = res.getDrawable(R.drawable.placeholderimage, null);
    } else {
        drawable = res.getDrawable(resID);
    }
    image.setImageDrawable(drawable);
}

Note:
getDrawable(int id) is now deprecated starting API 22.
In the sample code, I used getDrawable(int id, Resources.Theme theme) instead.
You might want to check out the other alternatives.

Is there any better way to load 4000 images from drawable in listview?

Try using Android's RecyclerView and/or 3rd-party libs such as Glide.

Community
  • 1
  • 1
Gino Mempin
  • 12,644
  • 19
  • 50
  • 69
  • It's better to handle the Resource.NotFoundException instead for checking the object itself. I just answer my own question. – Development Zeast Oct 21 '16 at 06:49
  • 1
    I would suggest to check the ID, this too many exception will cost time, especialy for (4000 tries) – AxelH Oct 21 '16 at 06:57
0
 Boolean fileFound = true;
        try{
            int resID = res.getIdentifier(mDrawableName , "drawable", activity.getPackageName());
            Drawable drawable = res.getDrawable(resID );
            image.setImageDrawable(drawable );
        }catch (Resources.NotFoundException e){
                fileFound = false;
        }
        if(!fileFound){
            int resID = res.getIdentifier("img_not_found" , "drawable", activity.getPackageName());
            Drawable drawable = res.getDrawable(resID );
            image.setImageDrawable(drawable );
        }
  • what's wrong with the complete answer of @ginomempin ? `You either catch that exception or check if getIdentifier returned 0`. By the way, an exception is a heavy process. So this methods will slow the process drasticly – AxelH Oct 21 '16 at 06:55
  • I just answer my own question. This code works like a charm for me. – Development Zeast Oct 21 '16 at 09:41
  • 1
    But why did you answer yourself if someone already did it, and with more explanations ? For the following users, this is better to have explanation... Just accept his answers since this is clearly what you have done. – AxelH Oct 21 '16 at 09:50
  • Yes @AxeIH you are right i am going to accept his answer. Although i resolve the issue before his answer. – Development Zeast Oct 21 '16 at 23:17