无法使用File://在WebView中打开本地PDF [英] Can't open a local PDF in webview using file://
问题描述
我需要在不使用Google文档的情况下在Webwiev中显示位于我的应用程序目录中的PDF文件.
I need to display a PDF file located in my app dir in a webwiev without using Google Docs.
这是我的网络视图代码:
This is my code for the webview:
public void OpenPDF() {
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file://mnt/data/data/com.example.android/files/Docs/test.pdf");;
}
我得到一个空白的Web视图,而不是下面的代码.
I get a blank webview instead of below code works
public void OpenPDF() {
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("file://mnt/data/data/com.example.android/files/Docs/test.html");
}
我在Web浏览器中直接使用 file://mnt/data/data/com.example.android/files/Docs/test.pdf
和 test.html 代码>,并且对两者都有效.我安装了PDF阅读器.
I tested directly in my web browser with file://mnt/data/data/com.example.android/files/Docs/test.pdf
and test.html
and it worked for both. I installed a PDF reader.
07-25 17:09:07.967: W/webcore(11347): java.lang.Throwable: EventHub.removeMessages(int what = 107) is not supported before the WebViewCore is set up.
07-25 17:09:07.967: W/webcore(11347): at android.webkit.WebViewCore$EventHub.removeMessages(WebViewCore.java:1683)
07-25 17:09:07.967: W/webcore(11347): at android.webkit.WebViewCore$EventHub.access$7900(WebViewCore.java:926)
07-25 17:09:07.967: W/webcore(11347): at android.webkit.WebViewCore.removeMessages(WebViewCore.java:1795)
07-25 17:09:07.967: W/webcore(11347): at android.webkit.WebView.sendOurVisibleRect(WebView.java:2917)
07-25 17:09:07.967: W/webcore(11347): at android.webkit.ZoomManager.setZoomScale(ZoomManager.java:593)
07-25 17:09:07.967: W/webcore(11347): at android.webkit.ZoomManager.access$1700(ZoomManager.java:49)
07-25 17:09:07.967: W/webcore(11347): at android.webkit.ZoomManager$PostScale.run(ZoomManager.java:984)
07-25 17:09:07.967: W/webcore(11347): at android.os.Handler.handleCallback(Handler.java:605)
07-25 17:09:07.967: W/webcore(11347): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 17:09:07.967: W/webcore(11347): at android.os.Looper.loop(Looper.java:137)
07-25 17:09:07.967: W/webcore(11347): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-25 17:09:07.967: W/webcore(11347): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 17:09:07.967: W/webcore(11347): at java.lang.reflect.Method.invoke(Method.java:511)
07-25 17:09:07.967: W/webcore(11347): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-25 17:09:07.967: W/webcore(11347): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-25 17:09:07.967: W/webcore(11347): at dalvik.system.NativeStart.main(Native Method)
关于为什么只有pdf文件无法打开的任何想法?
Any ideas about why only pdf files won't open ?
推荐答案
Their seems to be an answer to your question already in stack overflow at Open Local Html File in Webview - Android
总结一下:在实际路径之前使用file:///应该可以解决除.pdf文件以外的所有问题
to sum it up : Usage of file:/// before your actual path should solve your problem for everything except .pdf files
Webview在android :(.中不支持PDF.您需要根据不同的来源使用google doc.
Webview does not support PDF in android :(. You need to use google doc according to different sources.
这篇关于无法使用File://在WebView中打开本地PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!