Click here to Skip to main content
15,881,870 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
Java
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
Java
decodeStream(is)
instead
Java
decodeByteArray(data,0,length)
and remove code for publishProgress() all work correct. What is wrong in my code? Have somebody any idea? Help please!
Posted
Updated 1-May-12 22:13pm
v3

1 solution

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.
 
Share this answer
 
v2
Comments
Vsevywniy 24-Jul-12 4:21am    
Thanks so murch for answer. I'll be very glad to see an example.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900