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

不打扰是莪最后的温柔 提交于 2019-12-20 02:39:08

问题


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 .


回答1:


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.



来源:https://stackoverflow.com/questions/16875357/volley-networkimageview-sometimes-doesnt-show-the-error-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!