Volley - NetworkImageView sometimes doesn't show the error image?
Asked Answered
G

1

3

So I've decided to try out the new Volley library as shown on Google IO 2013.

I've tried it out while using the easy solution of NetworkImageView to show multiple images on a GridView.

It works nice and shows images, but if I let it download the images and then I turn off the WiFi during the download, it doesn't show an error as if everything still loads. Not only that, but if I restore the connection, it doesn't resume the loading.

Why does it occur, and how can I fix it? Maybe it's actually a bug?

Here's my sample code, if anyone wishes to try it out (BitmapCacheLru code here):

public class MainActivity extends Activity {
    private static final int COLUMNS_COUNT = 4;
    private RequestQueue _requestQueue;
    private ImageLoader _imageLoader;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        _requestQueue=Volley.newRequestQueue(this);
        _imageLoader=new ImageLoader(_requestQueue, new BitmapLruCache());

        final GridView gridView = new GridView(this);
        gridView.setNumColumns(COLUMNS_COUNT);
        final int screenWidth = getResources().getDisplayMetrics().widthPixels;

        gridView.setAdapter(new BaseAdapter() {
            @Override
            public View getView(final int position, final View convertView, final ViewGroup parent) {
                NetworkImageView rootView = (NetworkImageView) convertView;

                if (rootView == null) {
                    rootView = new NetworkImageView(MainActivity.this);
                    rootView.setLayoutParams(new AbsListView.LayoutParams(screenWidth / COLUMNS_COUNT, screenWidth / COLUMNS_COUNT));
                    rootView.setScaleType(ScaleType.CENTER_CROP);
                    rootView.setDefaultImageResId(android.R.drawable.sym_def_app_icon);
                    rootView.setErrorImageResId(android.R.drawable.ic_dialog_alert);
                }

                final String url = getItem(position);
                rootView.setImageUrl(url, _imageLoader);
                return rootView;
            }

            @Override
            public long getItemId(final int position) {
                return 0;
            }

            @Override
            public String getItem(final int position) {
                return Images.imageThumbUrls[position];
            }

            @Override
            public int getCount() {
                return Images.imageThumbUrls.length;
            }
        });

        setContentView(gridView);
    }

    @Override
    protected void onStop() {
        _requestQueue.cancelAll(this);
        super.onStop();
    }
}

P.S. If you want to see the code of NetworkImageView, I think it's available here .

Glockenspiel answered 1/6, 2013 at 17:35 Comment(3)
Looking at the Logcat, are there any error/debug messages from Volley when you turn off the Wifi?Packthread
actually i haven't checked it. does volley have a special log tag to look for?Glockenspiel
Yep, it is "Volley" as you may guess :-)Packthread
S
5

I think the problem is that the volley does not help you to reload the image.

A quick inspection shows that the NetworkImageView only loads data when onLayout method is called and the method loadImageIfNecessary will queue the network request if necessary.

When there is no Internet connection, the error callback will be called and there is no further action once the Internet get itself connected.

However, since you have the NetworkImage in a list, when you scroll the list, I suppose you will reuse the cell view and call setImageURL once again. If the Internet connection is available, the image will be loaded automatically. Alternatively, once the Internet connection is up, you can refresh the list view and so that the image will be loaded automatically.

Speedway answered 2/6, 2013 at 9:45 Comment(4)
it's a bug then, right? if so, what should i do in the code to fix it? is the latest link i've provided a good one to modify?Glockenspiel
Not tested, but I think you should listen for network changes yourself and invalidate the ListView once the user has connected.Cirrate
@Speedway When i scroll the listview and comes back then some images are missing which was shown while loading. Why is So??Loricate
@Loricate I am afraid that I may not understand your question. Normally when you scroll back, the cached image should appear immediately unless they are not cached at all.Speedway

© 2022 - 2024 — McMap. All rights reserved.