【问题标题】:Xamarin: Android WebView does not display local PDFXamarin:Android WebView 不显示本地 PDF
【发布时间】:2017-05-03 03:52:02
【问题描述】:

我有一个带有单个 web 视图的 Xamarin Android 应用程序。我想在该 web 视图中显示本地文件夹中的 PDF。

这是我的布局文件:

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

这是我的活动代码:

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

当我运行代码时,我看到一个空白页面。控制台调试输出显示了一些我无法真正与此特定事物相关的信息,但可能是某些信息:

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):视频/avc 无法识别的配置文件 2130706433 05-02 19:47:56.276 I/VideoCapabilities(4263):视频/mp4v-es 的配置文件 4 不受支持

按照 Android 文档中的建议,我已添加 INTERNET 权限。

是的,文件存在。在导航到此活动之前,我会执行 File.Exists 以检查它是否已下载。

【问题讨论】:

  • WebView 不支持 pdf。

标签: android xamarin


【解决方案1】:

Android WebView 无法像 iOS 那样读取本地 pdf 文件。 最好的办法是使用手机默认的 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);

希望这会有所帮助。

【讨论】:

  • 这真的很糟糕,我必须在外部阅读器中打开它,主要是因为它破坏了沉浸感。您的解决方案的后续问题:我必须在打开之前将其复制到公共区域吗?这将暴露 PDF ..
猜你喜欢
  • 1970-01-01
  • 2017-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多