简体   繁体   中英

Xamarin: Android WebView does not display local PDF

I have a Xamarin Android app with a single webview. I want to display a PDF from a local folder in that webview.

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

I have added INTERNET permission, as suggested in the Android documentation.

And yes, the file is present. I do a File.Exists prior to navigating to this activity to check if it has been downloaded.

Android WebView is not able to read local pdf files as iOS does. The best thing you can do is to open the file with the default PDF reader of the phone.

This is the code to open the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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