Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将PDF加载到android webview中_Android_Pdf_Webview - Fatal编程技术网

将PDF加载到android webview中

将PDF加载到android webview中,android,pdf,webview,Android,Pdf,Webview,在我的android应用程序中,我必须将服务器中的PDF加载到网络视图中。根据一个堆栈溢出问题,我在加载它时使用了googledocview。但我的问题是,有时它不会在webview中显示文件,而在另一个实例中,它显示得相当好。我想不出这个问题。但代码似乎没有问题。我的代码段如下 WebViewLable.getSettings().setJavaScriptEnabled(true); WebViewLable.getSettings().setLoadWithOverview

在我的android应用程序中,我必须将服务器中的PDF加载到网络视图中。根据一个堆栈溢出问题,我在加载它时使用了googledocview。但我的问题是,有时它不会在webview中显示文件,而在另一个实例中,它显示得相当好。我想不出这个问题。但代码似乎没有问题。我的代码段如下

WebViewLable.getSettings().setJavaScriptEnabled(true);    
    WebViewLable.getSettings().setLoadWithOverviewMode(true);
    WebViewLable.getSettings().setUseWideViewPort(true);  

    fullURL=GoogleDOCview+"https://livefarmer.com/labels/"+itemURL;
    WebViewLable.loadUrl(fullURL);

}

private class Callback extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(
            WebView view, String url) {
        return(false);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/app_bg_black"
    android:orientation="vertical" >

    <include
        android:id="@+id/nav_bar"
        android:layout_width="fill_parent"
        layout="@layout/nav_bar" />


    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webViewLable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:windowSoftInputMode="adjustResize" />

</LinearLayout>
我的网络视图如下

WebViewLable.getSettings().setJavaScriptEnabled(true);    
    WebViewLable.getSettings().setLoadWithOverviewMode(true);
    WebViewLable.getSettings().setUseWideViewPort(true);  

    fullURL=GoogleDOCview+"https://livefarmer.com/labels/"+itemURL;
    WebViewLable.loadUrl(fullURL);

}

private class Callback extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(
            WebView view, String url) {
        return(false);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/app_bg_black"
    android:orientation="vertical" >

    <include
        android:id="@+id/nav_bar"
        android:layout_width="fill_parent"
        layout="@layout/nav_bar" />


    <WebView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/webViewLable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:windowSoftInputMode="adjustResize" />

</LinearLayout>

这是未显示pdf时的logcat输出

这是logcat在webview中显示pdf时的输出

那么我的代码有什么问题。我怎样才能解决这个问题。 谢谢和问候

String PdfUrl = "http://pdfexample.com/abcd.pdf";
String url = "http://docs.google.com/gview?embedded=true&url=" + PdfUrl;
Log.i(TAG, "PDF URL : " + url);
webView.getSettings().setJavaScriptEnabled(true); 
webView.loadUrl(url);

试试这个

我解决上述问题的方法是在WebView完成加载后检查内容高度,如果其值为0,则尝试重新加载url

        myWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                if (view.getContentHeight() == 0) view.loadUrl(url);
            }
        });

有时显示,有时不显示。不,它始终显示,,请仅尝试此代码。不,亲爱的。它并不总是起作用。这些PDF只包含两到三页。它仍然有效。你对这个问题有什么想法吗?????有时它会正确加载PDF。但我无法跟踪错误。它在log-cat中没有显示错误消息。嗨,伙计们!这个线程已经很老了,但我面临着同样的问题:有时pdf不会显示在webview中(这似乎是一个非常随机的行为)。。你找到解决办法了吗?谢谢什么是
WebViewLable
?这是真的代码吗?什么是
GoogleDOCview
,什么是
itemURL
?是的。这是一个真正的代码。我现在正在做的。所以我不能发布整个代码,因为它不属于我。WebViewLable是我的webview。我正在加载的PDF是从服务器获取的。itemURL是这些文件的URL,GoogleDOCview是Google文档查看器。在显示或不显示pdf时,Logcat没有任何变化。。