flutter - 在android上显示带有颤动的pdf
问题描述
您好,下面的代码显示了一个带有颤振的 pdf 文件,请参阅上述 pub 包但是当我在 android 上运行代码时是否出现以下错误?如何解决上述问题: PDFDocument document = waitit PDFDocument.fromAsset (fullPath);
包装:advance_pdf_viewer
错误:
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Exception: Error parsing asset file!
E/flutter (32201): #0 PDFDocument.fromAsset (package:advance_pdf_viewer/src/document.dart:66:7)
颤振代码
Directory documentDirectory;
if (Platform.isAndroid) {
documentDirectory = await getExternalStorageDirectory();
}
else{
documentDirectory=await getApplicationDocumentsDirectory();
}
String documentPath = documentDirectory.path;
String fullPath = "$documentPath/example.pdf";
print("FullPath: "+fullPath);
File file = new File.fromUri(Uri.parse(fullPath));
PDFDocument document = await PDFDocument.fromFile(file);
Navigator.push(context, MaterialPageRoute(
builder: (context) => PdfPreviewScreen(file: document,path:fullPath,rp: rptemp,)
));
例外:
Unhandled Exception: Exception: Error parsing asset file!
E/flutter ( 4693): #0 PDFDocument.fromAsset (package:advance_pdf_viewer/src/document.dart:66:7)
解决方案
您正在从文件路径而不是资产文件(与应用程序数据一起存储)加载 pdf,因此您需要使用插件的特定方法而不是从您在您的代码中使用的资产加载 pdf马上。
File file = new File.fromUri(Uri.parse(fullPath));
PDFDocument doc = await PDFDocument.fromFile(file);
您的代码应如下所示:
Directory documentDirectory;
if (Platform.isAndroid) {
documentDirectory = await getExternalStorageDirectory();
}
else{
documentDirectory=await getApplicationDocumentsDirectory();
}
String documentPath = documentDirectory.path;
String fullPath = "$documentPath/example.pdf";
print("FullPath: "+fullPath);
File file = new File.fromUri(Uri.parse(fullPath));
PDFDocument document = await PDFDocument.fromFile(file);
Navigator.push(context, MaterialPageRoute(
builder: (context) => PdfPreviewScreen(file: document,path:fullPath,rp: rptemp,)
));
推荐阅读
- mysql - 如何在mysql中转换haversine公式的结果距离
- typescript - 抽象工厂或生成器类用于具有微小差异的相似对象?
- reactjs - 用于 OAuth 2 和 OIDC 的 React admin absolute redirectTo URL
- r - 使用 lme4 对具有 2 个索引变量的面板数据的混合效应模型?
- linux - 在第二个磁盘上安装 Manjaro 时出现一些错误 (UEFI+GPT)
- google-cloud-dataflow - 当在此之前添加 Reshuffle() 时,BatchElements() 适用于 DirectRunner,但不适用于 DataflowRunner
- github-api - 如何从 github 链接获取数据?
- node.js - Mongoose 在数据库中创建一个新文档,但在生产中不获取更新的集合
- macos - SwiftUI 中的 macOS 下拉视图?
- angular - MatIcon 仅在 sidenav 上显示文本