PDF文件加载到了Android的WebView [英] loading PDFs into the android webview

查看:301
本文介绍了PDF文件加载到了Android的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的机器人应用程序,我必须在服务器上的 PDF文件加载到的WebView 。根据我以前的谷歌DOC视图加载它一个堆栈溢出问题。但我的问题是有时它不会显示在文件中的的WebView ,而在另一种情况下它会显示得非常好。我想不通的问题。但code似乎是好的。我的code段如下图所示。

  WebViewLable.getSettings()setJavaScriptEnabled(真)。
    。WebViewLable.getSettings()setLoadWithOverviewMode(真);
    。WebViewLable.getSettings()setUseWideViewPort(真);    fullURL = GoogleDOCview +https://livefarmer.com/labels/+ itemURL;
    WebViewLable.loadUrl(fullURL);}私有类回调扩展WebViewClient {
    @覆盖
    公共布尔shouldOverrideUrlLoading(
            鉴于网页视图,字符串URL){
        返回(假);
    }
}

和我的WebView是如下图所示。

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=@绘制/ app_bg_black
    机器人:方向=垂直>    <包括
        机器人:ID =@ + ID / nav_bar
        机器人:layout_width =FILL_PARENT
        布局=@布局/ nav_bar/>
    <的WebView
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:ID =@ + ID / webViewLable
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:可聚焦=真
        机器人:focusableInTouchMode =真
        机器人:windowSoftInputMode =adjustResize/>< / LinearLayout中>

这是当它不显示PDF格式的logcat的输出。

这是当它显示web视图PDF中的logcat的输出。

因此​​,whats错误与我的code。我怎么能解决这个问题。
感谢和问候!


解决方案

 字符串PdfUrl =htt​​p://pdfexample.com/abcd.pdf;
字符串URL =htt​​p://docs.google.com/gview?embedded=true&url=+ PdfUrl;
Log.i(TAG,PDF URL:+网址);
webView.getSettings()setJavaScriptEnabled(真)。
webView.loadUrl(URL);

试试这个

in my android application I have to load PDFs in the server into a webview. according to a stack-overflow question I used Google DOC view in loading it. but my problem is some times it does not show the file in the webview while in another instance it displays quite well. I can't figure out the problem. but the code appears to be OK. my code segment is as below.

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);
    }
}

and my webview is as below.

<?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>

this is the logcat output when it not showing the pdf.

this is the logcat output when it showing the pdf in the webview.

So whats the wrong with my code. how can I fix this. Thanks and regards!

解决方案

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);

try this

这篇关于PDF文件加载到了Android的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆