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!

我在同一件事上遇到了一些问题.您是否尝试过在控制台中查找,也许返回了一些错误?有时,decodeStream会抛出异常并抛出outOfMemoryException,这意味着您试图从URL接收的图片太大,无法为设备的内存进行解析.

您可以尝试使用encodeStream并为流提供一些解码选项,这些选项将减小图片的大小,并且(希望)不会引发任何异常.

当我不得不做这样的事情时,我实际上遵循了以下解决方案:
-尝试获取图片而无需调整大小
-如果失败,请捕获异常,调用垃圾回收,然后尝试将图片的大小调整为1/4的大小
-如果同样失败,请执行与上述相同的操作,但是将图片调整为大小的1/16(这没问题,无论如何我必须显示的图片都不大于100/100像素)
-如果其他所有方法都失败,则只需显示一个占位符即可.

现在,实际上一切都变得更加复杂(我也使用了一些繁重的图片缓存,您可以尝试阅读并尝试集成this[^] .我已经使用了它,它的工作原理就像是一种魅力).

我现在没有用家庭计算机编写的代码示例,但是如果我记得明天或接下来的几天,我将带一个示例.
I''ve had some problems with the same thing. Have you tried looking in the console, maybe some errors are returned? Sometimes decodeStream throws and outOfMemoryException, meaning that the picture you are trying to receive from the URL is too large to parse for the device''s memory.

You can try a decodeStream and provide the stream with some decode options that will decrease the picture size, and (hopefully) not throw any exceptions.

When I had to do something like this, I actually went with a solution along the lines of:
- Try and get the picture without resizing
- If this fails, catch the exception, call a garbage collect, and try to resize the picture at 1/4 the size
- If that fails as well, do the same as above, but resizing the picture at 1/16 the size (that was no problem, the picture I had to display wasn''t larger than 100/100 pixels anyway)
- If all else fails, just display a placeholder.

Now, it was all more complicated in fact (I''ve used some heavy picture caching as well, you can try reading and trying to integrate this[^]. I''ve used it and it worked like a charm).

I have no code example to post right now, as I''m writing this from my home computer, but if I remember tomorrow, or the next few days, I''ll come back with a sample.