Android Universal Image Loader java.io.FileNotFoundException: http:/xxx/lxx/xxxx.jpg
前段时间在使用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();
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!