我有這個webview代碼,我想讓用戶點擊PDF鏈接時可以打開PDF文件。這裏是代碼,你能告訴我我必須把這個內容放在PDF區嗎?我嘗試了很多不同的方式,並且無法看到PDF。謝謝您的幫助。Android - 在webview中加載PDF
try
{
Intent intentUrl = new Intent(Intent.ACTION_VIEW);
intentUrl.setDataAndType(url, "application/pdf");
intentUrl.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myActivity.startActivity(intentUrl);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(myActivity, "No PDF Viewer Installed", Toast.LENGTH_LONG).show();
}
雖然我沒有嘗試過這個已經:作爲
webview.setWebViewClient (new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// do your handling codes here, which url is the requested url
// probably you need to open that url rather than redirect:
if (url.startsWith("tel:")) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
} else if (url.startsWith("mailto:")) {
url = url.replaceFirst("mailto:", "");
url = url.trim();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("plain/text").putExtra(Intent.EXTRA_EMAIL,
new String[]{url});
startActivity(i);
} else if (url.startsWith("geo:")) {
try {
} catch (Exception e) {
System.out.println(e);
}
} else if (url.endsWith("pdf")) {
try {
} catch (Exception e) {
System.out.println(e);
}
} else {
view.loadUrl(url);
}
return true;
// then it is not handled by default action
}
});
是否可以在不運行異步下載任務的情況下在webview上顯示PDF文件? – user1363871
不,與iOS上的UIWebView不同,Android WebView不顯示PDF內容。你可以嘗試直接調用intent(即沒有AsyncDownloadTask),儘管無論如何你都會離開你的活動。 – CSmith
嗯,所以在android中最好的方法是調用異步下載文件並保存。你知道這個過程是否可以自動找到文件名並保存爲SD卡上的名字?我在使用這個時總是指定了名字。謝謝! – user1363871