您好我正在尝试通过DownloadManager下载视频文件和pdf。对于pdf它的工作正常,但当我试图下载视频时,它没有下载。
我正在使用以下代码:
private void downloadFileFromServer(String fid, String title, String uri) {
boolean isPdf = uri.matches(".*\\b.pdf\\b.*");
if (isPdf) {
fileName = fid + ".pdf";
} else {
fileName = fid + ".mp4";
}
File file = new File(getActivity().getExternalFilesDir("DDocs/")
+ "/Files/" + fileName);
if (file.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (isPdf) {
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
} else {
intent.setDataAndType(Uri.fromFile(file), "video/mp4");
}
startActivity(intent);
} else if (BUtil.isOnline(getActivity())) {
downloadManager = (DownloadManager) getActivity().getSystemService(
DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(uri);
DownloadManager.Request request = new DownloadManager.Request(
Download_Uri);
request.addRequestHeader(BConstant.WEB_SERVICES_COOKIES,
cookie);
request.addRequestHeader(BConstant.WEB_SERVICES_TOKEN_HEADER,
token);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE);
request.setTitle(title);
request.setDestinationInExternalFilesDir(getActivity(),
"DDocs/Files", fileName);
if (!file.exists()) {
progressBar = new ProgressDialog(getActivity());
progressBar.setCancelable(false);
progressBar.setMessage(BConstant.LODING);
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.show();
lastDownload = downloadManager.enqueue(request);
}
} else if (!BUtil.isOnline(getActivity())) {
ToastUserMessage.message(getActivity(),
BUserMessage.FIRST_TIME_LOAD_MESSAGE);
}
}