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 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)
instead
decodeByteArray(data,0,length)
and remove code for publishProgress() all work correct. What is wrong in my code? Have somebody any idea? Help please!