【问题标题】:Xamarin Android Opening PDF fileXamarin Android 打开 PDF 文件
【发布时间】:2020-05-31 08:37:31
【问题描述】:

我使用 Xamarin.Forms 4.6.0.800

我在我的 ContentPage 的构造函数中使用以下代码在 Android 上查看 PDF 文件。

        // Get access to the local file system
        var localFileService = DependencyService.Get<ILocalFileService>();

        // Save the PDF to a temporary file
        var temporaryFile = localFileService.SaveToTempPDFFile(reportName, pdfData);

        // Set the WebView to reference the temporary PDF file
        PdfView.Source = $"file:///android_asset/pdfjs/web/viewer.html?file={temporaryFile}";

XAML 很简单:

<ContentPage.Content>
    <WebView 
        x:Name="PdfView" 
        VerticalOptions="Fill" 
        HorizontalOptions="Fill" />
</ContentPage.Content>

这已经工作了一段时间,但是今天重新测试时,我收到以下错误:

file:///Android_asset/pdfjs/web/viewer.html?file=/data/user/0/com.myApp/cache/reportname.pdf could not be loaded because:

net::ERR_FILE_NOT_FOUND

最近 Xamarin 中是否发生了一些变化导致此问题?与此同时,我正在寻找可能导致此错误的可能已更改的内容(因为它已经运行了很长一段时间了)。

【问题讨论】:

    标签: xamarin.forms xamarin.android


    【解决方案1】:

    我找到了导致它的原因,但不知道为什么。在 Android 项目中,我将 Platform Target 设置为 x64。改回 Any CPU 就又能正常工作了???

    它再次出现,但只有当我将它安装在 Pixel 4(我的测试手机)上时。模拟器没问题。

    【讨论】:

    • 尝试在我的 Galaxy S8 上加载 pdf 时遇到同样的错误。我也在使用 pdfjs,我的代码和你的非常相似。和你一样,我的代码在模拟器中运行,并且我的代码在我今天测试之前一直运行良好。
    【解决方案2】:

    我只是通过重建项目、存档和测试 App Store 中的 APK 来实现它。

    我注意到当 ERR_FILE_NOT_FOUND 错误发生时 APK 文件变小了。好的 APK 在文件大小上更接近我上一个好的 APK(从 2020 年 1 月开始)。只是猜测,也许存档过程中的优化导致了这个?

    我只能通过 App Store 的安装来验证 PDF 是否正在显示。在模拟器中进行测试时,PDF 可以正常显示,并且可以直接使用手机构建。

    【讨论】:

      猜你喜欢
      • 2017-08-05
      • 2013-07-01
      • 2020-06-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-03-27
      相关资源
      最近更新 更多