码迷,mamicode.com
首页 > 其他好文 > 详细

使用universal-image-loader中出现的EOFException解决方法

时间:2014-12-04 12:11:27      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:android   universal-image-load   eofexception   

直接贴代码

public class HttpClientImageDownloader extends BaseImageDownloader implements
		ImageDownloader {

	public HttpClientImageDownloader(Context context) {
		super(context);
	}
	@Override
	protected InputStream getStreamFromFile(String imageUri, Object extra)
			throws IOException {
		String filePath = Scheme.FILE.crop(imageUri);
		File file = new File(filePath);
		if(!file.exists())
			file.createNewFile();
		return new ContentLengthInputStream(new BufferedInputStream(new FileInputStream(filePath), BUFFER_SIZE),
				(int) new File(filePath).length());
	}
	@SuppressLint("UseValueOf")
	@Override
	protected InputStream getStreamFromNetwork(String imageUri, Object extra)
			throws IOException {
//		HttpURLConnection conn = createConnection(imageUri, extra);
//		
//		int redirectCount = 0;
//		while (conn.getResponseCode() / 100 == 3 && redirectCount < MAX_REDIRECT_COUNT) {
//			conn = createConnection(conn.getHeaderField("Location"), extra);
//			redirectCount++;
//		}<span style="font-family: Arial, Helvetica, sans-serif;">//EOFException来自conn.getResponseCode()</span>

//		InputStream imageStream;
//		try {
//			imageStream = conn.getInputStream();
//		} catch (IOException e) {
//			// Read all data to allow reuse connection (http://bit.ly/1ad35PY)
//			IoUtils.readAndCloseStream(conn.getErrorStream());
//			throw e;
//		}
		DefaultHttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet(imageUri);
		request.getParams().setParameter("http.socket.timeout",
				new Integer(connectTimeout));
		request.addHeader("Pragma", "no-cache");
		request.addHeader("Cache-Control", "no-cache");
		request.addHeader("Charset", "UTF-8");
		HttpResponse httpResponse = client.execute(request);
		HttpEntity entity = httpResponse.getEntity();
		InputStream imageStream = entity.getContent();
		return new ContentLengthInputStream(new BufferedInputStream(imageStream, BUFFER_SIZE), (int)entity.getContentLength());
	}

}
另外配置为自己的imageloader
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
				.defaultDisplayImageOptions(defaultOptions)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.denyCacheImageMultipleSizesInMemory()
				.threadPoolSize(3)
				.imageDownloader(new HttpClientImageDownloader(this ))
				.tasksProcessingOrder(QueueProcessingType.LIFO)
				.memoryCache(new WeakMemoryCache())
				.build();


使用universal-image-loader中出现的EOFException解决方法

标签:android   universal-image-load   eofexception   

原文地址:http://blog.csdn.net/leibinleibin/article/details/41721461

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!