public class HttpDownloader extends AsyncTask<String, String, String> {
private String HttpURL;
private String DirPath;
private String FileName;
private String FileFath;
private android.os.Handler handler;
public HttpDownloader(String HttpURL, String FilePath, android.os.Handler handler) {
this.HttpURL = HttpURL;
this.DirPath = FilePath.substring(0, FilePath.lastIndexOf('/'));
this.FileName = FilePath.substring(FilePath.lastIndexOf('/') + 1, FilePath.length());
this.FileFath = FilePath;
this.handler = handler;
File Dir = new File(DirPath);
if(!Dir.exists()) {
Dir.mkdir();
}
if(new File(DirPath + "/" + FileName).exists()) {
new File(DirPath + "/" + FileName).delete();
}
this.execute();
}
@Override
protected String doInBackground(String... strings) {
int count;
try {
URL url = new URL(HttpURL);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
OutputStream output = new FileOutputStream(FileFath);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
}
catch(Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
}
@Override
protected void onPostExecute(String file_url) {
handler.sendEmptyMessage(0);
}
}
요렇게 코드를 작성했습니다.
다운로드가 성공적으로 되기는 하는데요. 파일 크기는 70Mb인데 시간이 한 5분은 걸립니다. 그리고
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/art: Suspending all threads took: 12.884ms
W/art: Suspending all threads took: 18.204ms
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/art: Suspending all threads took: 6.110ms
이렇게 에러가 찍힙니다.
어떤 부분이 잘못인지라도 알 수 있으면 좋을 것 같은데 어디가 잘못인지도 모르겠습니다.
그리고 디버깅은 삼성 갤럭시 S5를 가지고 하고 있습니다.