本文介绍了Volley - NetworkImageView有时不显示错误图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 所以我决定试用新的 Volley图书馆,如 Google IO 2013 。 我在使用 NetworkImageView 的简单解决方案,在 GridView 上显示多个图像时,已经尝试过了。 它工作不错,显示图像,但如果我让它下载图像,然后在下载过程中关闭WiFi,它不会显示错误,就像一切还在加载不仅如此,如果我恢复连接,它不会恢复加载。 为什么会发生,我该如何解决?也许这实际上是一个bug? 这是我的示例代码,如果有人想尝试( BitmapCacheLru 代码这里): 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,新的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); 返回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(); } } 如果你想看到 NetworkImageView 的代码,我想可以使用这里。解决方案> 我觉得这个问题是这个排球不能帮你重新加载图片。 快速检查显示,只有在OnLayout方法被调用时,NetworkImageView 才会加载数据,并且方法 loadImageIfNecessary 将排队网络请求,如有必要。 当没有互联网连接时,将调用错误回调,一旦Internet连接到互联网,就不会有进一步的操作。 但是,由于您在列表中有NetworkImage,因此当您滚动列表时,我想您将重新使用单元格视图并再次调用setImageURL。如果Internet连接可用,图像将自动加载。或者,一旦Internet连接启动,您可以刷新列表视图,以便图像将自动加载。 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 . 解决方案 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. 这篇关于Volley - NetworkImageView有时不显示错误图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-05 19:20