Не удается открыть PDF-файл в flutter

#flutter #pdf #dart

#flutter #PDF #dart

Вопрос:

Я хочу открыть pdf файл, используя пакет open_file.

 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 
  

Комментарии:

1. Я не уверен, но является ли ABC-2745.pdf20200818-5566-1byemw9.pdf допустимым именем файла?

2. @Типу Да. Я могу открыть https://xxx/uploads/organization_file/ABC-2745.pdf20200818-5566-1byemw9 в браузере.

Ответ №1:

Я решил это, добавив приведенный ниже код в 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 «