我使用AsyncTask從互聯網下載約50 MB文件。有時候,當我下載這個文件時,進度條增益非常慢(即使在我使用Wi-Fi時)。在一分鐘後,手機顯示我,下載完成,但文件本身只有〜100kB,沒有更多。但是當我重新啓動設備,並嘗試下載文件時,下載過程會短暫而快速地執行。有沒有人遇到同樣的問題?在下載新文件之前是否需要清除相同的下載內存?我正在將文件下載到Environment.externalStoryDirectory()。AsyncTask - 緩慢下載
THX
從活動中調用下載:
mProgressDialog = new ProgressDialog(ItemDetails.this);
mProgressDialog.setTitle("Downloading");
mProgressDialog.setMessage("Downloading sth...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
DownloadMapTask downloadFile = new DownloadMapTask(ItemDetails.this);
downloadFile.execute(web_location_url);
mProgressDialog.show();
下載異步任務(兩種方法):
@Override
protected String doInBackground(String... urls) {
int count;
PATH=maps_loc+"/Android/data/test/maps/";
try {
URL url = new URL(urls[0]);
HttpURLConnection connection2 = (HttpURLConnection) url.openConnection();
connection2.setRequestMethod("GET");
connection2.setDoOutput(true);
connection2.connect();
int lenghtOfFile = connection2.getContentLength();
File apkdir = new File(PATH);
apkdir.mkdirs();
File newInstall = new File(PATH, name+".tmp");
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(newInstall);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1 && running==true) {
total += count;
publishProgress((int) (total * 100/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void onProgressUpdate(Integer... args) {
ItemDetails.mProgressDialog.setProgress(args[0]);
}
可以請你告訴我們一些代碼...? – 2013-02-14 11:13:23
當然,在一分鐘內 – Waypoint 2013-02-14 11:14:47
你也可以檢查你的日誌。它可能是gc或其他東西。 – 18446744073709551615 2013-02-14 11:15:55