flutter - 无法在颤动中打开pdf文件
问题描述
我想使用open_file包打开一个pdf
文件。
OpenFile.open('/data/user/0/xxx/app_flutter/ABC-2745.pdf20200818-5566-1byemw9.pdf');
错误
E/flutter ( 9713): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference, null)
编辑
我从服务器获取 url,并将其存储在本地
generatePDF(dynamic value) async {
print(value);
final filename = value.substring(value.lastIndexOf("/") + 1);
var request = await HttpClient().getUrl(Uri.parse(value));
var response = await request.close();
var bytes = await consolidateHttpClientResponseBytes(response);
String dir = (await getApplicationDocumentsDirectory()).path;
File file = new File('$dir/$filename.pdf');
await file.writeAsBytes(bytes);
print(file.path);
try {
OpenFile.open(file.path);
} catch (e) {
print(e.toString());
}
}
输出
I/flutter (11889): https://xxx/uploads/organization_file/ABC-2745.pdf20200818-5566-1byemw9
I/flutter (11889): /data/user/0/xxx/app_flutter/ABC-2745.pdf20200818-5566-1byemw9.pdf
E/flutter (11889): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(error, Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference, null)
E/flutter (11889): #0 StandardMethodCodec.decodeEnvelope
解决方案
我通过在AndroidManifest.xml
.
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileProvider" android:exported="false" android:grantUriPermissions="true" tools:replace="android:authorities">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" tools:replace="android:resource" />
</provider>
xmlns:tools="http://schemas.android.com/tools"
推荐阅读
- java - 为什么图像没有上传到 Firebase 存储和缓存?
- r - 来自迭代过滤的子集(列的级别)
- angular - 如何在树结构中将数据与 Angular 中的相同元素组合
- php - 使用 Laravel 验证现有电子邮件
- java - 嵌套的 FIrebaseRecyclerAdapter
- html - 使用 -webkit-search-cancel-button 时真正隐藏的清除图标
- flutter - MainAxisSize.SpaceEvenly 没有在小部件中提供相等的空间
- python - 为列熊猫数据框中的第一个/最大的值变化按组创建虚拟对象
- asp.net - Stripe - 如何根据卡是欧盟卡还是非欧盟卡收取附加费
- python - 在接受命令行输入的同时从 python 运行 C 程序