DownloadManager 没有开始下载文件

DownloadManager doesn't start to download file

假设我要下载这个文件(随机文件): http://www.analysis.im/uploads/seminar/pdf-sample.pdf

这是我的代码:

DownloadManager.Request req = new DownloadManager.Request(Uri.parse("http://www.analysis.im/uploads/seminar/pdf-sample.pdf"));

req.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
   .setAllowedOverRoaming(false)
   .setTitle("Random title")
   .setDescription("Random description")
   .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "pdf-sample.pdf");

在调试模式下,我可以看到所有参数都是正确的,为什么下载没有开始?

编辑

我当前的权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

您允许在DownloadManager.Request.NETWORK_MOBILE的网络类型下下载,但为什么设置setAllowedOverRoaming(false)

我尝试使用 Downloadmanager 下载文件,这是我的代码:

String url = "http://example.com/large.zip";
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

// only download via WIFI
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
request.setTitle("Example");
request.setDescription("Downloading a very large zip");

// we just want to download silently
request.setVisibleInDownloadsUi(false);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
request.setDestinationInExternalFilesDir(context, null, "large.zip");

// enqueue this request
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
downloadID = downloadManager.enqueue(request);

希望对您有所启发。

按照以下步骤下载随机文件。 创建一个名为 DownloadFileAsync.java

的 class
  public class DownloadFileAsync extends AsyncTask<String, String, String> {
  public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
  private ProgressDialog mProgressDialog;

  private Context context;

  public DownloadFileAsync(Context context) 
 {
this.context = context;
 mProgressDialog = new ProgressDialog(context);
 mProgressDialog.setMessage("Downloading file..");
 mProgressDialog.setIndeterminate(false);
 mProgressDialog.setMax(100);
 mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
 mProgressDialog.setCancelable(true);

}

@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}

@Override
protected String doInBackground(String... aurl) {

try {
    File root = Environment.getExternalStorageDirectory();
    URL u = new URL(aurl[0]);
    HttpURLConnection c = (HttpURLConnection) u.openConnection();
    c.setRequestMethod("GET");
    c.setDoOutput(true);
    c.connect();

    int lenghtOfFile = c.getContentLength();

    FileOutputStream f = new FileOutputStream(new File(root + "/", aurl[1]));

    InputStream in = c.getInputStream();

    byte[] buffer = new byte[1024];
    int len1 = 0;
    long total = 0;

    while ((len1 = in.read(buffer)) > 0) {
        total += len1; //total = total + len1
        publishProgress("" + (int)((total*100)/lenghtOfFile));
        f.write(buffer, 0, len1);
    }
    f.close();
} catch (Exception e) {
    Log.d("Downloader", e.getMessage());
}

return null;

}

protected void onProgressUpdate(String... progress) {
 Log.d("ANDRO_ASYNC",progress[0]);
 mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
}


    }

在你的 activity 中调用上面的 class 作为

  new DownloadFileAsync(TransformerActivity.this).execute(Constants.VIDEO_DOWNLOAD_LINK,"9T51B0108.mp4");

还要确保您没有在 phone 上启用任何 VPN 应用程序。似乎其中一些应用程序会干扰 DownloadManager。

您只准备了DownloadManager.Request。如代码片段所示调用 enqueue 将实际开始下载

DownloadManager downloadManager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
long downloadID = downloadManager.enqueue(request);// enqueue puts the download request in the queue.

查看 DownloadManager 的完整示例 here