0
我試圖下載使用下載管理文件內的的AsyncTask下載文件,下載管理器的Android
private class DownloadTask extends AsyncTask<String, Void, Boolean>
{
private Context mContext;
public DownloadTask(Context context)
{
mContext = context;
}
@Override
protected Boolean doInBackground(String... strings) {
String fileName = strings[2]+"_"+strings[3]+ strings[4];
String destination = mDestination + fileName;
final Uri uri = Uri.parse("file://" + destination);
mDownloading = true;
//If the file is already downloading just return.
File file = new File(destination);
if (file.exists()) {
return true;
}
//set downloadmanager
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(strings[0]));
request.setDescription(mContext.getString(R.string.downloading)+ " "+strings[1]);
request.setTitle(mContext.getString(R.string.downloading_title));
//set destination
request.setDestinationUri(uri);
// get download service and enqueue file
final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
final long downloadId = manager.enqueue(request);
//set BroadcastReceiver to enable next download
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
unregisterReceiver(this);
mDownloading = false;
}
};
//register receiver for when file download is compete
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
return true;
}
}`
的代碼時,我傳遞文件的URL工作正常。問題是,我們想對PHP上的WEB進行GET調用。此WEB方法創建或選擇文件並使用標題重定向("Location: ".$database->single()['Url']);
但是,當我們從DownloadManager進行調用時,它只是立即調用registerReceiver。
有沒有人知道爲什麼會發生這種情況?
如果我們使用HttpURLConnection
它可以正常工作,但我們希望將所有下載的辛苦工作委託給DownloadManager。
謝謝您的意見。
沒有答案,但嘗試下載使用此libraray:https://github.com/tonyofrancis/Fetch?utm_source=android-arsenal.com&utm_medium=referral&utm_campaign=5196 –
謝謝,我會盡力。 – Gabrielkdc
不幸的是,對於我來說,我使用Fetch得到了完全相同的結果。儘管發現很大。 – Gabrielkdc