前段时间在使用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的更多相关文章

  1. java.io.FileNotFoundException: D:\xxx\yyy (拒绝访问。)问题

    File file=new File(fileAllName); FileWriter fw=new FileWriter(file); 在Java的 FileWriter 方法时 系统抛出了异常 j ...

  2. freemarker报 java.io.FileNotFoundException:及TemplateLoader使用

    使用过freemarker的肯定其见过如下情况: java.io.FileNotFoundException: Template xxx.ftl not found. 模板找不到.可能你会认为我明明指 ...

  3. Android java.io.FileNotFoundException:*** (Permission denied)

    自从离开上海来到杭州之后,陆续接触了Android,Python,C++,OpenCV,最为一个新人来说,一路上遇到了很多坑,幸运的是 这互联网的强大(大傻子我还是蛮喜欢的哈),一路填填补补总算走了下 ...

  4. Android代码混淆的问题解决(java.io.FileNotFoundException)

    Android Studio(2.3.3) 在给代码混淆时,提示: Warning:Exception while processing task java.io.FileNotFoundExcept ...

  5. Android下载文件提示文件不存在。。。 java.io.FileNotFoundException

    遇到这个错误java.io.FileNotFoundException,事实上文件是存在的,把地址复制到手机浏览器都能够直接下载的,但为嘛不能下载呢. Error in downloadBitmap ...

  6. android universal image loader 缓冲原理详解

    1. 功能介绍 1.1 Android Universal Image Loader Android Universal Image Loader 是一个强大的.可高度定制的图片缓存,本文简称为UIL ...

  7. keytool 错误: java.io.FileNotFoundException: 拒绝访问

    keytool 错误: java.io.FileNotFoundException: 拒绝访问 打开命令行,切换到D:\AndroidStudioProjects\MyApplication\app目 ...

  8. Caused by: java.io.FileNotFoundException

    1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  9. java.io.FileNotFoundException: antlr-2.7.7.jar (系统找不到指定的路径。)[待解决]

    严重: Failed to destroy the filter named [struts2] of type [org.apache.struts2.dispatcher.ng.filter.St ...

随机推荐

  1. node 跨域问题

    node跨域有很多方法 1.引入 中间件cors 我觉的最好的方法 var express=require('express'); var cors=require('cors'); var app= ...

  2. 前端面试题整理——javaScript部分

    (1)typeof 和 instanceof 1.typeof 对于基本数据类型(boolean.null.undefined.number.string.symbol)来说,除了 null 都可以显 ...

  3. 在vue配置sass

    先npm两个插件 npm install sass-loader --save-dev npm install node-sass --save-dev 然后在webpack当中配置 { test: ...

  4. web全栈架构师[笔记] — 03 html5新特性

    HTML5新特性 一.geolocation PC端 精度比较低 通过IP库定位 移动端 通过GPS window.navigator.geolocation 单次 getCurrentPositio ...

  5. call/apply以及this指向的理解

    javascript是面向对象的语言,Function也是一种对象,有自己的属性和方法.call和apply就是js函数自带方法,挂在Fucntion.prototype上. 一般调用某函数时,直接“ ...

  6. 表单元素的required,autocomplete,list用法

    required: 当在一个表单元素如:input中加上required属性时,点击提交表单按钮,针对input会弹出一个默认的警告信息,如下图: 代码如下: <form id="my ...

  7. 《R数据挖掘入门》彩色插图(第9章)

    图9.5  图9.9

  8. Python数据清洗基本流程

    # -*- coding: utf-8 -*-"""Created on Wed Jul 4 18:40:55 2018 @author: zhen"" ...

  9. 洗礼灵魂,修炼python(39)--面向对象编程(9)—魔法方法表

    好的,不多说,大招来了,几乎完整的魔法方法: 基本的魔法方法 Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Defi ...

  10. Arch Linux 更新源(以清华 arch 源为例)

    Arch Linux 编辑­/etc/pacman.d/mirrorlist,在文件最顶端添加: Server = https://mirrors.tuna.tsinghua.edu.cn/archl ...