새로운 건 아니고 몇 가지 보완점이 필요해서 정리해봅니다.
http://dmh11.tistory.com/150
위 사이트에 있는 소스를 기초로 했고, 약간의 수정이 필요한 부분이 있습니다.
HttpDownloader.java에서
protected DownloadFileCompleted doInBackground(String... params) ==> 요 메소드로 가시면 아래 부분이 있을거예요.
long totalBytes = entity.getContentLength();
long
readBytes =
0
;
byte
[] buffer =
new
byte
[MAX_BUFFER_SIZE];
while
(totalBytes > readBytes) {
int
read = bis.read(buffer);
readBytes += read;
publishProgress(readBytes , totalBytes);
bos.write(buffer,
0
, read);
}
여기서 while안의 조건을 주목해주세요. totalBytes는 해당 파일의 사이즈죠. 근데 이 사이즈를 못 얻어올 경우가 꽤 있어요.
그럴 땐 저 조건에 들어가지 못해서 파일 다운로드를 수행하지 못하죠. 그럼 안되잖아요.
그래서 저러지 못할 경우 또 하나를 추가했습니다. 아래처럼요.
if (totalBytes > -1)
{
while(totalBytes > readBytes) {
read = bis.read(buffer); ==> read변수를 메소드의 처음에 선언했어요.
readBytes += read;
Log.d("dd", "readBytes = " + readBytes);
publishProgress(readBytes , totalBytes);
bos.write(buffer, 0, read);
}
} else if (totalBytes == -1)
{
while((read = bis.read(buffer)) != -1) {
//int read = bis.read(buffer);
readBytes += read;
Log.d("dd", "readBytes = " + readBytes);
publishProgress(readBytes , totalBytes);
bos.write(buffer, 0, read);
}
totalBytes = readBytes; ==> totalBytes는 readBytes의 최종 누적값이 들어가게 됩니다.
publishProgress(readBytes , totalBytes); ==> 프로그레스로 보내줘야겠죠.
}
http://www.softwarepassion.com/android-series-download-files-with-progress-dialog/
위 사이트를 참고했습니다.
저처럼 삽질하시는 분들이 있을것 같아서 삽질 그만하시라고 올립니다 ㅎㅎ;;
else if (totalBytes == -1)
{
while((read = bis.read(buffer)) != -1) {
//int read = bis.read(buffer);
readBytes += read;
Log.d("dd", "readBytes = " + readBytes);
publishProgress(readBytes , totalBytes);
bos.write(buffer, 0, read);
}
totalBytes = readBytes; ==> totalBytes는 readBytes의 최종 누적값이 들어가게 됩니다.
publishProgress(readBytes , totalBytes); ==> 프로그레스로 보내줘야겠죠.
}
여기에서 while문안에 publishProgress(readBytes , totalBytes); 는 의미가 없지않은가요..
totalBytes 는 -1인데..