안녕하세요.
현재 저에게 주어진건 파일다운로드가 가능한 링크 뿐입니다.
요걸 가지고 폰 내부 혹은 외부 메모리등에 저장하는 기능을 만들고 있는데요..
현재 사용하고 있는건 HttpURLConnection 클래스 입니다.
찾아보니 downloadManager라는 클래스도 있더군요.
api레벨 2.3부터 사용이 가능하고 다운로드를 편하게(?)할 수 있다는 설명이 적혀있네요..
현재는 아래와 같이 AsyncTask와 HttpURLConnection를 사용해서 파일을 받고있습니다.
(중간에 progress는 프로그레스바를 진행시키기 위해 짜놓은 것입니다.)
파일 용량은 최소 50MB~최대 1.2GB정도 됩니다..
이런 대용량 파일을 받을 때 어느 쪽이 더 효율(?)적이고 관리하기 좋은지 궁금합니다.
conn = (HttpURLConnection) NewUrl.openConnection();
conn.setRequestProperty("Accept-Encoding", "identity");// add
long fileSize = conn.getContentLength();// 다운받을 파일 크기
if (fileSize < 0) {
conn.disconnect();
suc = 1;
}
else if (emptyStorage <= fileSize) {
suc = 4;
} else {
df = file.createFile("*/*", "filename");//따옴표안이 파일네임.
fosFile = getContentResolver().openOutputStream(df.getUri());
byte[] data = new byte[1024];// lenth
int read;
long total = 0;
int progress = 0;
InputStream is = conn.getInputStream();
for (;;) {
read = is.read(data);
total += read;
int progress_temp = (int) ((double) total / fileSize * 100);
publishProgress("" + progress_temp);
if (progress_temp % 10 == 0 && progress != progress_temp) {
progress = progress_temp;
}
if (read <= 0) {
break;
}
fosFile.write(data, 0, read);
if (isCancelled())
{
Log.e("취소되었다", "취소되었다");
return 5;
}
}
is.close();
fosFile.close();
conn.disconnect();