5

我试图在 webview 中加载一个 url。网址链接到网站上的 pdf 文件。我想在 webview 中显示这个 pdf 文件。出于某种原因,我只看到一个白屏并且 pdf 没有加载。

这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Webkit;

namespace TrackandTrace.Droid
{
    [Activity (Label = "PodActivity")]          
    public class PodActivity : Activity
    {
        public string PodUrl { get; set; }


        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.PodLayout);

            var PodWebView = FindViewById<WebView> (Resource.Id.webViewPod);
            PodUrl = Intent.GetStringExtra ("PodUrlString" ?? "No Pod Data available");

            PodWebView.Settings.AllowFileAccess = true;
            PodWebView.Settings.JavaScriptEnabled = true;
            PodWebView.Settings.BuiltInZoomControls = true;
            PodWebView.LoadData (PodUrl);

            PodWebView.SetWebViewClient (new PodWebViewClient ());

            // Create your application here
        }

        private class PodWebViewClient : WebViewClient
        {
            public override bool ShouldOverrideUrlLoading (WebView view, string url)
            {
                view.LoadUrl (url);
                return true;
            }
        }
    }
}
4

3 回答 3

2

我找到了一种适合我的应用程序的方法,并且仍然在“webView”中打开 pdf 下面是代码:

PodWebView.LoadUrl ("http://docs.google.com/viewer?url=" + PodUrl);

这不像在 IOS webview 中那样流畅,但是这样无论您拥有什么设备,您都可以打开它。

于 2013-04-29T14:08:17.747 回答
1

我认为不可能在 WebView 中加载 PDF。至少这是我从阅读有关同一问题的其他 SO 问题中得到的印象。

我个人会像这样使用默认的 PDF 阅读器:

var intent = new Intent(Intent.ActionView);
intent.SetDataAndType(uri, "application/pdf");
intent.SetFlags(ActivityFlags.ClearTop);
StartActivity(intent);
于 2013-04-29T10:48:18.907 回答
0

您必须将此链接添加到您的 pdf 链接,或者只需在下面粘贴相同的代码

WebView webview = FindViewById<WebView>(Resource.Id.webView1);
        urlPdf = ("example of url");
        WebSettings settings = webview.Settings;
        settings.JavaScriptEnabled = true;
        webview.LoadUrl("http://drive.google.com/viewerng/viewer?embedded=true&url=" + urlPdf);

在 urlPDF“ http://drive.google.com/viewerng/viewer?embedded=true&url= ”之前添加此链接很重要

于 2017-06-05T12:52:03.467 回答