1
@Override
public void run() {
try {
downloadTaskObserver.onPendingStarted();
File dir = new File(Environment.getExternalStorageDirectory(), RMConfig.RM_FOLDER);
dir.mkdirs();
String sFileName = dir + "/" + trackInfo.getTrackMetadata().getTrackId() + ".mp3";
File root = new File(sFileName);
if (!root.exists()) {
root.createNewFile();
} else {
return;
}
HttpGet mHttpGet = new HttpGet(trackInfo.getMediaUrl());
HttpResponse response = WSManager.getInstalnce().getRHHttpsClient().execute(mHttpGet);
InputStream zipStream = response.getEntity().getContent();
FileOutputStream fOut = new FileOutputStream(root);
long fileSize = Long.valueOf(response.getFirstHeader("Content-Length").getValue());
long byteCount = 0;
byte[] buffer = new byte[4096];
int bytesRead = -1;
downloadTaskObserver.onDownloadStarted();
while ((bytesRead = zipStream.read(buffer)) != -1 && !cancelDownload) {
fOut.write(buffer, 0, bytesRead);
byteCount += bytesRead;
int value = (int) (byteCount * 100L/fileSize);
downloadTaskObserver.onUpdateProgress(value);
}
if (fOut != null) {
fOut.flush();
fOut.close();
}
if (zipStream != null) {
zipStream.close();
}
if (!cancelDownload) {
downloadTaskObserver.onDownloadFinished();
}
} catch (Exception e) {
e.printStackTrace();
downloadTaskObserver.onDownloadError();
}
}
public interface DownloadTaskObserver {
public void onPendingStarted();
public void onDownloadStarted();
public void onDownloadFinished();
public void onDownloadCanceled();
public void onUpdateProgress(int percent);
public void onDownloadError();
}
public void addToDownloadQueue(GetTrackPlaybackInfo trackInfo, DownloadTaskObserver downloadTaskObserver) {
DownloadTask downloadTask = new DownloadTask(trackInfo, downloadTaskObserver);
mDownloadTasks.add(downloadTask);
// mDownloadTasksPool.execute(downloadTask);
new Thread(downloadTask).start();
}
但是每當我開始下載另一個線程時,它似乎已經完成。這裏有什麼可能是錯誤的,我沒有看到?預先感謝您的幫助。下一個下載線程中斷前一個:爲什麼?
I/BrowseActivity(17104): Thread-16 onPendingStarted...
I/BrowseActivity(17104): Thread-16 onDownloadStarted...
E/BrowseActivity(17104): percent 0
E/BrowseActivity(17104): percent 10
E/BrowseActivity(17104): percent 20
Thread-16 onDownloadFinished...
I/BrowseActivity(17104): Thread-17 onPendingStarted...
I/BrowseActivity(17104): Thread-17 onDownloadStarted...
E/BrowseActivity(17104): percent 0
E/BrowseActivity(17104): percent 10
向線程添加更多日誌記錄,以便您可以檢查其工作流程。 –