Android Universal Image Loader java.io.FileNotFoundException: http:/xxx/lxx/xxxx.jpg
时间:2014-05-22 04:48:45
收藏:0
阅读:493
前段时间在使用ImageLoader异步加载服务端返回的图片时总是出现
java.io.FileNotFoundException: http://xxxx/l046/10046137034b1c0db0.jpg at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177) at com.nostra13.universalimageloader.core.download.URLConnectionImageDownloader.getStreamFromNetwork(URLConnectionImageDownloader.java:40) at com.nostra13.universalimageloader.core.download.ImageDownloader.getStream(ImageDownloader.java:27) at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.saveImageOnDisc(LoadAndDisplayImageTask.java:296) at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.tryLoadBitmap(LoadAndDisplayImageTask.java:204) at com.nostra13.universalimageloader.core.LoadAndDisplayImageTask.run(LoadAndDisplayImageTask.java:128) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
这样的异常。
刚开始没明白怎么回事,于是就开始疯狂的google。最后终于在stackoverflow发下了这个问题的解决方案。这是原文http://stackoverflow.com/questions/14305765/filenotfoundexception-with-universal-image-loader
出现这个错误的原因是:服务端使用的是Apache 提供的开源组件HttpGet和HttpPost。而ImageLoader默认使用的是java再带的原生组件URLHttpConnection。因此在接收和发送数据时会出现解析异常。
解决办法:Android客户端在初始化ImageLoader时将默认的URLHttpConnection改为DefaultHttpClient。
主要代码:
ImageLoaderConfiguration config = new ImageLoaderConfiguration .Builder(MainActivity.sharedMainActivity.getApplicationContext()) .defaultDisplayImageOptions(defaultOptions) .discCache(new UnlimitedDiscCache(cacheDir)) .threadPoolSize(1) .memoryCache(new WeakMemoryCache()) .imageDownloader(new HttpClientImageDownloader(new DefaultHttpClient(manager, params))) .build();
完整初始化代码:
HttpParams params = new BasicHttpParams(); // Turn off stale checking. Our connections break all the time anyway, // and it‘s not worth it to pay the penalty of checking every time. HttpConnectionParams.setStaleCheckingEnabled(params, false); // Default connection and socket timeout of 10 seconds. Tweak to taste. HttpConnectionParams.setConnectionTimeout(params, 10 * 1000); HttpConnectionParams.setSoTimeout(params, 10 * 1000); HttpConnectionParams.setSocketBufferSize(params, 8192); // Don‘t handle redirects -- return them to the caller. Our code // often wants to re-POST after a redirect, which we must do ourselves. HttpClientParams.setRedirecting(params, false); // Set the specified user agent and register standard protocols. HttpProtocolParams.setUserAgent(params, "some_randome_user_agent"); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ClientConnectionManager manager = new ThreadSafeClientConnManager(params, schemeRegistry); ImageLoaderConfiguration config = new ImageLoaderConfiguration .Builder(MainActivity.sharedMainActivity.getApplicationContext()) .defaultDisplayImageOptions(defaultOptions) .discCache(new UnlimitedDiscCache(cacheDir)) .threadPoolSize(1) .memoryCache(new WeakMemoryCache()) .imageDownloader(new HttpClientImageDownloader(new DefaultHttpClient(manager, params))) .build();
Android Universal Image Loader java.io.FileNotFoundException: http:/xxx/lxx/xxxx.jpg,布布扣,bubuko.com
评论(0)