2

我需要在不使用 Google Docs 的情况下在 webwiev 中显示位于我的应用程序目录中的 PDF 文件。

这是我的 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");;
}

我得到一个空白的 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.html");  
}

我直接在我的网络浏览器中进行了测试file://mnt/data/data/com.example.android/files/Docs/test.pdftest.html并且它对两者都有效。我安装了一个PDF阅读器。

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 文件无法打开的任何想法?

4

2 回答 2

3

Their seems to be an answer to your question already in stack overflow at Open Local Html File in Webview - Android

to sum it up : Usage of file:/// before your actual path should solve your problem for everything except .pdf files

Webview does not support PDF in android :(. You need to use google doc according to different sources.

于 2013-07-25T14:43:52.333 回答
-1

I think you should try with 3 slashes, like that:

webview.loadUrl("file:///mnt/data/data/com.example.android/files/Docs/test.pdf");
于 2013-07-25T14:44:29.707 回答