本文介绍了android图片下载器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用函数(在下面定义)通过http下载图像时,在某些情况下会获得空值(解码器->解码返回false),但在某些情况下函数可以正常工作.网址在所有情况下都是正确的.帮我解决这个错误.这是代码:

when i use function(it defines below) for download image via http i get null value(decoder->decode returned false) in some cases but in some cases function works correct. Url is correct in all cases. Help me to solve this bug. Here is code:

private void getImage(String uri, int p){
    		try{
    			int[] helper = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    			int[] help = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    			URL url = new URL(uri);
    			HttpURLConnection httpCon = (HttpURLConnection)url.openConnection();
    			httpCon.setDoInput(true);
    			httpCon.connect();
    			if(httpCon.getResponseCode() != 200)
					throw new Exception("Failed");
    			InputStream is = httpCon.getInputStream();
    			int length = httpCon.getContentLength();
    			int portionLength = (length/SIZE);
    			final byte[] data = new byte[length];
    			//int read = is.read(data,0,length);
    			int downloaded = 0;
    			for(int j = 1; j <  SIZE; j++){
				int read = is.read(data, downloaded, portionLength);
				downloaded += read;
				helper[p] = (100/SIZE)*j;
				publishProgress(helper[0],helper[1],helper[2],helper[3],helper[4],
						helper[5],helper[6],helper[7],helper[8],helper[9],
						helper[10],helper[11],helper[12],helper[13],helper[14],
						helper[15],helper[16],helper[17],helper[18],helper[19]);
			}
			is.read(data,downloaded,length-downloaded);
			help[p] = 100;
			publishProgress(help[0],help[1],help[2],help[3],help[4],
					help[5],help[6],help[7],help[8],help[9],
					help[10],help[11],help[12],help[13],help[14],
					help[15],help[16],help[17],help[18],help[19]);
			httpCon.disconnect();
			is.close();
			bMapArr[p] = BitmapFactory.decodeByteArray(data,0,length);
    		}
    		catch(Exception e){}
    	}


而且,当我使用


Moreover,when i use

decodeStream(is)

代替

decodeByteArray(data,0,length)

,并删除publishProgress()的代码都可以正常工作.我的代码有什么问题?有任何想法吗?

and remove code for publishProgress() all work correct. What is wrong in my code? Have somebody any idea? Help please!

推荐答案


这篇关于android图片下载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 09:52
查看更多