[英]File download using Android downloads provider
I am trying to use webview to download an email attachment in my application. 我正在尝试使用Webview在我的应用程序中下载电子邮件附件。 I follow the procedure that the default browser application follows as below: 1) Write MyWebViewClient that implements DownloadListener and override onDownloadStart. 我遵循默认浏览器应用程序遵循的过程,如下所示:1)编写实现DownloadListener的MyWebViewClient并重写onDownloadStart。 webview calls setDownloadListener passing instance of MyWebViewClient 2) To download, I create instance of ContentValues, fillit up with values as follows: webview调用传递MyWebViewClient实例的setDownloadListener的实例2)要下载,我创建ContentValues实例,并按如下所示填充值:
ContentValues values = new ContentValues();
values.put("uri", webAddress.toString());
values.put("cookiedata", cookies);
values.put("useragent", userAgent);
values.put("notificationpackage",
getPackageName());
values.put("notificationclass",
DownloadReceiver.class.getCanonicalName());
values.put("visibility", 0);
values.put("mimetype", mimetype);
values.put("hint", filename);
values.put("description", webAddress.mHost);
values.put("is_public_api", Boolean.TRUE);
values.put("destination", 4);
if (contentLength > 0) {
values.put("total_bytes", contentLength);
}
final Uri contentUri =
getContentResolver().insert(Uri.parse("content://downloads/my_downloads"), values);
Note that as I cannot import android.provider.Downloads, therefore I have to provide actual string values from Downloads.java as above. 请注意,由于我无法导入android.provider.Downloads,因此如上所述,我必须从Downloads.java提供实际的字符串值。 Somehow download is not working with this code. 某种程度上下载无法与此代码一起使用。 Also, I had to put value for "is_public_api" and also set "destination" as program goes through checkInsertPermissions function of DownloadProvider.java which uses enforceAllowedValues to check these values. 另外,当程序通过DownloadProvider.java的checkInsertPermissions函数时,我必须放置“ is_public_api”的值,并还要设置“目标”,该函数使用forcelAllowedValues来检查这些值。 I cannot put the destination to external sdcard where I would like to download. 我无法将目标放置到要下载的外部sdcard上。 I see that default Android browser is not setting the values for "is_public_api" and "destination" (see BrowserActivity.java). 我看到默认的Android浏览器未设置“ is_public_api”和“目的地”的值(请参见BrowserActivity.java)。 How does that work for default browser? 默认浏览器如何工作? If I don't put value for these values then I get Security Exception. 如果我不为这些值输入值,则会收到“安全性异常”。
I have setup "android.permission.ACCESS_DOWNLOAD_MANAGER" permission in my manifest. 我在清单中设置了“ android.permission.ACCESS_DOWNLOAD_MANAGER”权限。
您还具有写入清单中设置的SD卡的权限吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.