use*_*658 14 pdf android android-webview
我正在访问一个有.pdf文件的网站.如果我通过Web浏览器打开该文档,它会开始下载它.如果我通过webview打开它,没有任何反应.我应该将什么设置应用于webview以使其开始下载?
我已经有了这个.
wvA.setDownloadListener(new DownloadListener()
{
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimeType,
long size)
{
Intent viewIntent = new Intent(Intent.ACTION_VIEW);
viewIntent.setDataAndType(Uri.parse(url), mimeType);
try
{
startActivity(viewIntent);
}
catch (ActivityNotFoundException ex)
{
}
}
});
Run Code Online (Sandbox Code Playgroud)
Sil*_*ria 24
实现下载侦听器以处理任何类型的下载:
webView.loadUrl(uriPath);
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
}
});
Run Code Online (Sandbox Code Playgroud)
Leo*_*dos 18
您应该创建WebViewClient并将其设置为您的webview.每次单击链接时,都会调用WebViewClient的shouldOverrideUrlLoading方法.检查该URL是否指向pdf文件并执行您想要的操作.例如,您可以查看pdf.
webView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if (url.endsWith(".pdf")) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
// if want to download pdf manually create AsyncTask here
// and download file
return true;
}
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22217 次 |
| 最近记录: |