使用DownloadManager
,我正在尝试从我的应用下载文件,但无法下载任何文件。我在多个设备上重新创建了此问题API 16 or below
。任何高于该API的工作都可以。
会发生什么:
每次都会发生这种情况!请帮忙!
private static void downloadFile(BaseActivity activity, FileInfo fileContent) {
if (!DownloadManagerUtil.isDownloadManagerEnabled(activity)) {
DownloadManagerDialog.newInstance().show(activity.getSupportFragmentManager(),
DownloadManagerDialog.TAG);
return;
}
if (!FileUtil.hasStorage(true)) {
ToastUtil.showLong(R.string.external_storage_error);
return;
}
try {
final DownloadManager.Request request = DownloadManagerUtil.createRequest(
Uri.parse(fileContent.getResourceUrl()), fileContent.getTitle());
final DownloadManager downloadManager
= (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
ToastUtil.showShort(R.string.download_notification);
} catch (IllegalArgumentException e) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
ToastUtil.showLong(R.string.download_not_supported_on_old_androids);
} else {
throw new IllegalArgumentException(e);
}
}
}
public static DownloadManager.Request createRequest(Uri uri, String fileName) {
final DownloadManager.Request request = new Request(uri);
request.setAllowedNetworkTypes(Request.NETWORK_MOBILE | Request.NETWORK_WIFI);
request.setAllowedOverRoaming(false);
request.setTitle(fileName);
final File fullPath = new File(FileUtil.getDownloadFolderFullPath());
if (!fullPath.exists() && !fullPath.isDirectory()) {
fullPath.mkdirs();
}
final String path = FileUtil.getDownloadFolder();
request.setDestinationInExternalPublicDir(path, fileName);
if (DeviceUtil.isNewerThanGingearBread()) {
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.allowScanningByMediaScanner();
}
return request;
}