首页 > 解决方案 > 无法在颤动中打开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 

标签: flutterpdfdart

解决方案


我通过在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"


推荐阅读