private class test extends AsyncTask<String, String, String> {
private ProgressDialog mDlg;
@Override
protected String doInBackground(String... params) {
String suc = "F";
int count = 0;
long now = System.currentTimeMillis();
String localPath = myPath + now + ".mp3";
InputStream isFile = null;
FileOutputStream fosFile = null;
try {
URL myUrl = new URL(pinUrl3);
URI uri = new URI(myUrl.getProtocol(), myUrl.getUserInfo(), myUrl.getHost(), myUrl.getPort(),
myUrl.getPath(), myUrl.getQuery(), myUrl.getRef());
myUrl = uri.toURL();
// URL imgUrl = new URL(pinUrl);
// ������ �����ϴ� Ŭ���̾�Ʈ ��ü ����
HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
conn.setRequestProperty("Accept-Encoding", "identity");//add
conn.setConnectTimeout(2000);
int fileSize = conn.getContentLength();// 다운받을 파일 크기
double kilobytes = (fileSize / 1024);
double megabytes = (kilobytes / 1024);
Log.e("다운로드파일사이즈:", megabytes + "MB");
if (fileSize < 0) {
conn.disconnect();
suc = "F";
} else {
byte[] data = new byte[fileSize];
InputStream is = conn.getInputStream();
File file = new File(localPath);
if (!file.exists())
{
FileOutputStream fos = new FileOutputStream(file);
int read;
long total=0;
int progress = 0;
for (;;)
{
read = is.read(data);
total += read;
int progress_temp = (int) total * 100 / fileSize;
publishProgress(""+ progress_temp);
if (progress_temp % 10 == 0 && progress != progress_temp)
{
progress = progress_temp;
}
if (read <= 0)
{
break;
}
fos.write(data, 0, read);
}
is.close();
fos.close();
conn.disconnect();
suc = "T";
}
}
} catch (ConnectTimeoutException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return suc;
}
private void publishProgress(String string)
{
// TODO Auto-generated method stub
Log.e("publishProgress",Integer.parseInt(string)+"");
mDlg.setProgress(Integer.parseInt(string));
}
위와 같이 HttpURLConnection ,url을 사용해서 파일을 다운로드 받고,
진행상황을 보여주기 위해 프로그레스바도 만들었습니다.
용량은 최소 80mb 정도부터 시작하고요..
테스트 해보니 파일 다운로드는 정상적으로 작동 하는데, 프로그레스바로 넘겨주는
값이 음수값으로 출력됩니다; 그래서 중간에 바가 다시 줄어들어버리는 현상이 발생합니다.