想象一下,我想下载此文件(随机文件): 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"/>
答案 0 :(得分:3)
您允许以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);
我希望你受到启发。
答案 1 :(得分:1)
您只准备了DownloadManager.Request。如片段中所示调用enqueue实际上将开始下载
DownloadManager downloadManager= (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
long downloadID = downloadManager.enqueue(request);// enqueue puts the download request in the queue.
查看DownloadManager here的完整示例
答案 2 :(得分:0)
按照以下步骤下载随机文件。 创建一个名为 DownloadFileAsync.java
的类 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();
}
}
将您的活动中的上述课程称为
new DownloadFileAsync(TransformerActivity.this).execute(Constants.VIDEO_DOWNLOAD_LINK,"9T51B0108.mp4");
答案 3 :(得分:0)
还要确保手机上没有启用任何VPN应用程序。似乎其中一些应用程序可能会干扰DownloadManager。