简体   繁体   English

Xamarin:Android WebView不显示本地PDF

[英]Xamarin: Android WebView does not display local PDF

I have a Xamarin Android app with a single webview. 我有一个带有单个Webview的Xamarin Android应用。 I want to display a PDF from a local folder in that webview. 我想从该Web视图中的本地文件夹中显示PDF。

Here is my layout file: 这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />
  <WebView
    android:id="@+id/pdfContainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</LinearLayout>

And here is my Activity code: 这是我的活动代码:

 [Activity(Label = "ViewPdfActivity", ParentActivity = typeof(IssueDetailsActivity))]
public class ViewPdfActivity : Activity
{
    private WebView pdfContainer;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.ViewPdf);

        this.pdfContainer = this.FindViewById<WebView>(Resource.Id.pdfContainer);
        this.pdfContainer.SetBackgroundColor(Color.Black);
        this.pdfContainer.SetWebChromeClient(new WebChromeClient());

        var toolbar = this.FindViewById<Toolbar>(Resource.Id.toolbar);
        this.SetActionBar(toolbar);

        this.ActionBar.SetDisplayHomeAsUpEnabled(true);

        var pdfUrl = Intent.GetStringExtra("PdfUrl");
        this.LoadPdf(pdfUrl);
    }

    private void LoadPdf(string pdfUrl)
    {
        var file = $"file://{System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)}/{pdfUrl}";

        this.pdfContainer.LoadUrl(file);

        this.pdfContainer.SetWebChromeClient(new WebChromeClient());
    }
}

When I run the code, I see an empty page. 运行代码时,我看到一个空白页。 The console debug output is showing some info that I cannot really relate to this particular thing, but could nontheless be something: 控制台调试输出显示了一些我无法真正与该特定事物相关联的信息,但仍然可能是:

05-02 19:47:56.104 E/libEGL ( 4263): validate_display:99 error 3008 (EGL_BAD_DISPLAY) 05-02 19:47:56.172 W/VideoCapabilities( 4263): Unrecognized profile 2130706433 for video/avc 05-02 19:47:56.276 I/VideoCapabilities( 4263): Unsupported profile 4 for video/mp4v-es 05-02 19:47:56.104 E / libEGL(4263):validate_display:99错误3008(EGL_BAD_DISPLAY)05-02 19:47:56.172 W / VideoCapabilities(4263):无法识别的配置文件2130706433用于视频/ AVC 05-02 19: 47:56.276 I / VideoCapabilities(4263):video / mp4v-es不受支持的配置文件4

I have added INTERNET permission, as suggested in the Android documentation. 我已按照Android文档中的建议添加了INTERNET权限。

And yes, the file is present. 是的,该文件存在。 I do a File.Exists prior to navigating to this activity to check if it has been downloaded. 在导航到此活动之前,我先执行File.Exists来检查它是否已下载。

Android WebView is not able to read local pdf files as iOS does. Android WebView无法像iOS一样读取本地pdf文件。 The best thing you can do is to open the file with the default PDF reader of the phone. 最好的办法是使用手机的默认PDF阅读器打开文件。

This is the code to open the pdf: 这是打开pdf的代码:

        //Copy the private file's data to the EXTERNAL PUBLIC location
        var externalPath = Environment.ExternalStorageDirectory.Path + "/{name of your pdf file}.pdf";
        File.WriteAllBytes(externalPath, pdfDocument);

        Java.IO.File file = new Java.IO.File(externalPath);
        file.SetReadable(true);
        Android.Net.Uri uri = Android.Net.Uri.FromFile(file);
        Intent intent = new Intent(Intent.ActionView);
        intent.SetDataAndType(uri, "application/pdf");
        intent.SetFlags(ActivityFlags.ClearWhenTaskReset | ActivityFlags.NewTask);

        this.StartActivity(intent);

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM