如何解决无法在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,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”
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。