[英]Display pdf when link clicked in WebView android
I'm using a Web View to display a site inside my app, and that site contains links to PDF files.我正在使用 Web 视图在我的应用程序中显示一个网站,该网站包含指向 PDF 文件的链接。 But when this links are pressed the Web View displays a blank page instead of the PDF files.
但是,当按下此链接时,Web 视图显示一个空白页面,而不是 PDF 文件。 Is there anyway I can get it to display the PDF files correctly?
无论如何我可以让它正确显示 PDF 文件吗?
Thanks in advance.提前致谢。
You can create an intent to open the pdf files: 您可以创建打开pdf文件的意图:
@Override
public boolean launchPDF(WebView view, String url) {
if ( urlIsPDF(url)){
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "application/pdf");
try{
view.getContext().startActivity(intent);
} catch (ActivityNotFoundException e) {
//user does not have a pdf viewer installed
}
} else {
webview.loadUrl(url);
}
return true;
}
And then whenever a user clicks a PDF link in a page within your webview, the file will open in an external PDF app. 然后,只要用户单击webview中页面中的PDF链接,该文件就会在外部PDF应用程序中打开。
Or you can use Google Docs to launch them: 或者您可以使用Google文档启动它们:
String googleDocs = "https://docs.google.com/viewer?url=";
String pdf_url = "http://www.somedomain.com/new.pdf";
webView.loadUrl(googleDocs + pdf_url);
Remember to use the Internet Permission. 请记住使用Internet权限。
Simple to opne doc/pdf etc 简单的op / doc / pdf等
public static void openFile(Context context, String url) throws IOException {
// Create URI
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW);
// Check what kind of file you are trying to open, by comparing the url with extensions.
// When the if condition is matched, plugin sets the correct intent (mime) type,
// so Android knew what application to use to open the file
if (url.toString().contains(".doc") || url.toString().contains(".docx")) {
// Word document
intent.setDataAndType(uri, "application/msword");
} else if(url.toString().contains(".pdf")) {
// PDF file
intent.setDataAndType(uri, "application/pdf");
} else if(url.toString().contains(".ppt") || url.toString().contains(".pptx")) {
// Powerpoint file
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
} else if(url.toString().contains(".xls") || url.toString().contains(".xlsx")) {
// Excel file
intent.setDataAndType(uri, "application/vnd.ms-excel");
} else if(url.toString().contains(".zip") || url.toString().contains(".rar")) {
// WAV audio file
intent.setDataAndType(uri, "application/x-wav");
} else if(url.toString().contains(".rtf")) {
// RTF file
intent.setDataAndType(uri, "application/rtf");
} else if(url.toString().contains(".wav") || url.toString().contains(".mp3")) {
// WAV audio file
intent.setDataAndType(uri, "audio/x-wav");
} else if(url.toString().contains(".gif")) {
// GIF file
intent.setDataAndType(uri, "image/gif");
} else if(url.toString().contains(".jpg") || url.toString().contains(".jpeg") || url.toString().contains(".png")) {
// JPG file
intent.setDataAndType(uri, "image/jpeg");
} else if(url.toString().contains(".txt")) {
// Text file
intent.setDataAndType(uri, "text/plain");
} else if(url.toString().contains(".3gp") || url.toString().contains(".mpg") || url.toString().contains(".mpeg") || url.toString().contains(".mpe") || url.toString().contains(".mp4") || url.toString().contains(".avi")) {
// Video files
intent.setDataAndType(uri, "video/*");
} else {
//if you want you can also define the intent type for any other file
//additionally use else clause below, to manage other unknown extensions
//in this case, Android will show all applications installed on the device
//so you can choose which application to use
intent.setDataAndType(uri, "*/*");
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
I'm using webview_flutter plugin for my webview. This is the solution I applied.我正在为我的 webview 使用 webview_flutter 插件。这是我应用的解决方案。 In the initialUrl you can check if the url is a pdf link and use google doc to open it.
在 initialUrl 中,您可以检查 url 是否为 pdf 链接并使用 google doc 打开它。
initialUrl: widget.link!.indexOf('.pdf') != -1 ? 'https://docs.google.com/gview?
embedded=true&url=${widget.link}'
: widget.link!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.