setDestinationInExternalPublicDir方法

本文介绍了 Android 中 DownloadManager 的使用方法,包括如何设置下载路径及文件名。解释了 setDestinationUri, setDestinationInExternalFilesDir 和 setDestinationToSystemCache 等方法的区别,并指出这些方法仅适用于单个文件的下载。

download manager的setDestinationInExternalPublicDir(“Trinea”, “MeiLiShuo.apk”);

表示设置下载地址为sd卡的Trinea文件夹,文件名为MeiLiShuo.apk。


设置下载路径接口为setDestinationUri,setDestinationInExternalFilesDir,setDestinationToSystemCache。其中setDestinationToSystemCache仅限系统app使用。
这几个方法其实都是下载单个的不能下载目录,这几个方法是说我们保存下载的路径不一样而已
要整个目录下载只能去遍历下一个一个的下载
WebView 中无法下载的问题一般是因为没有设置相应的下载配置,可以尝试以下解决方法: 1. 在 AndroidManifest.xml 中添加权限: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 2. 设置 WebChromeClient,并重写 onDownloadStart 方法: ```java WebView webView = findViewById(R.id.webView); webView.setWebChromeClient(new WebChromeClient() { @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setMimeType(mimetype); String cookies = CookieManager.getInstance().getCookie(url); request.addRequestHeader("cookie", cookies); request.addRequestHeader("User-Agent", userAgent); request.setDescription("Downloading file..."); request.setTitle(URLUtil.guessFileName(url, contentDisposition, mimetype)); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(url, contentDisposition, mimetype)); DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); dm.enqueue(request); Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_LONG).show(); } }); ``` 重写 onDownloadStart 方法后,点击 WebView 中的下载链接时,会调用该方法进行下载。 注意:需要在 AndroidManifest.xml 中添加 DOWNLOAD_SERVICE 权限。 ```xml <uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" /> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值