@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