안녕하세요? 이렇게 항상 질문만 드리고 있군요 ^^;;;
이미지 다운로드 프로그램을 만들었습니다.
그런데.. 어떤 이미지들은 잘 받아지고..어떤 이미지들은 잘 안받아지네요..
특히 다음 부분에서 5~10분가량 멈춰있습니다.
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK){
어쩔때는 5~10분 뒤에 이미지를 받을때도 있습니다.
궁금한 것은 분명히 아래와 같이 타임아웃을 줬는데도 저렇게 오랫동안 안받아지는 이유가 뭘까요??
conn.setConnectTimeout(3000);
conn.setReadTimeout(6000);
이것은 에뮬에서 돌린결과구요. 실제폰에서는 그냥 에러가 나서 예외로 빠져버릴때가 많았습니다.
스레드로 제작했습니다.
아~ 2주일동안 붙잡고 있어도 모르겠습니다 OTL
// 이미지 다운로드
boolean DownloadImage(String Url, String FileName) {
URL imageurl;
HttpURLConnection conn = null;
FileOutputStream fos = null;
InputStream is = null;
try {
Log.d(TAG, "이미지다운-URL:"+Url);
imageurl = new URL("Url);
Log.d(TAG, "이미지다운-openConnection");
conn = (HttpURLConnection)imageurl.openConnection();
if(conn != null){
Log.d(TAG, "이미지다운-openConnection_ok");
conn.setConnectTimeout(3000);
conn.setReadTimeout(6000);
conn.setUseCaches(false);
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK){
Log.d(TAG, "이미지다운-getResponseCode");
int len = conn.getContentLength();
int raster_Length;
byte[] raster = new byte[len];
Log.d(TAG, "이미지다운-FileOpen:"+len);
is = conn.getInputStream();
fos = openFileOutput(FileName, 0);
for (;;) {
raster_Length = is.read(raster);
Log.d(TAG, "이미지다운-getStream:"+raster_Length);
if (raster_Length <= 0) break;
fos.write(raster,0, raster_Length);
}
}else{ Log.d(TAG, "openConnection_err2"); }
}else{ Log.d(TAG, "openConnection_err1"); }
Log.d(TAG, "이미지다운-종료");
} catch (Exception e) {
Log.d(TAG, "이미지다운-Error:"+e.getMessage());
return false;
} finally {
Log.d(TAG, "이미지다운-FileClose");
try { fos.flush(); } catch (IOException e) { e.printStackTrace(); }
try { is.close(); } catch (IOException e) { e.printStackTrace(); }
try { fos.close(); } catch (IOException e) { e.printStackTrace(); }
conn.disconnect();
}
return true;
}