lubritto / pdf_viewer_plugin

A Flutter plugin for IOS and Android providing a simple way to display PDFs.
MIT License
57 stars 64 forks source link

Error trying to retrieve local directory to write file. #7

Open leonardopaim opened 5 years ago

leonardopaim commented 5 years ago

I am Brazilian and I used google translator translation for this request:

I am trying to render a PDF on my App Flutter screen and at the moment the getApplicationDocumentsDirectory() method is called on the first line of loadPdf() the error described below is being returned.

In some attempts to get around the problem I saw that this method only returns the error when I have lib pdf_viewer_plugin imported into my project. If this lib is not imported the getApplicationDocumentsDirectory() method that is part of path_provider works normally.

(Device model used in development: Xiaomi Mi A1)

CODE:

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pdf_viewer_plugin/pdf_viewer_plugin.dart';

void main() {
  runApp(App());
}

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.indigo,
        primaryColor: Colors.blue,
      ),
      title: "Title App",
      home: NFCePage(),
    );
  }
}

class NFCePage extends StatefulWidget {
  @override
  _NFCePageState createState() => _NFCePageState();
}

class _NFCePageState extends State<NFCePage> {
  String pathPdf = "";

  @override
  void initState() {
    super.initState();
    loadPdf();
  }

  void loadPdf() async {
    final directory = await getApplicationDocumentsDirectory();
    final path = directory.path;
    final file = File('$path/nfce.pdf');
    ByteData bd = await rootBundle.load('assets/nfce.pdf');
    file.writeAsBytes(bd.buffer.asUint8List());

    bool arquivoExiste = await file.exists();
    if (arquivoExiste) pathPdf = file.path;

    if (!mounted) return;

    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Expanded(
              child: Container(
                height: 600,
                color: Colors.amber,
                child: PdfViewer(
                  filePath: pathPdf,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  buildPdfViewer() {
    return Container(
      child: PdfViewer(
        filePath: pathPdf,
      ),
    );
  }
}

ERROR:

D/com.shockwave.pdfium.PdfiumCore(15455): Starting PdfiumAndroid 1.9.0
E/PDFView (15455): load pdf error
E/PDFView (15455): java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)
E/PDFView (15455):  at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:313)
E/PDFView (15455):  at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:211)
E/PDFView (15455):  at com.github.barteksc.pdfviewer.source.FileSource.createDocument(FileSource.java:37)
E/PDFView (15455):  at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:53)
E/PDFView (15455):  at com.github.barteksc.pdfviewer.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:27)
E/PDFView (15455):  at android.os.AsyncTask$2.call(AsyncTask.java:333)
E/PDFView (15455):  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
E/PDFView (15455):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/PDFView (15455):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/PDFView (15455):  at java.lang.Thread.run(Thread.java:764)
leonardopaim commented 5 years ago

I was able to work around the issue by changing the widget that surrounds PDFViewer ().

it was like this:

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            path != null
                ? Flexible(
                    fit: FlexFit.loose,
                    flex: 1,
                    child: Container(
                      child: PdfViewer(
                        filePath: path,
                      ),
                    ),
                  )
                : Flexible(
                    fit: FlexFit.loose,
                    flex: 1,
                    child: Container(
                      child: Text("O PDF não está carregado"),
                    ),
                  ),
            listaDeOpcoesMinimalista(),
          ],
        ),
      ),
    );
  }

I didn't understand how the previous mode hampers screen loading. If you have any advice that better my implementation I would be grateful.

lubritto commented 5 years ago

@leonardopaim Hey, I saw that your problem is the need for defining a valid path to works, I will try to validate the path before load to fix this.

leonardopaim commented 5 years ago

@lubritto Thank you, I will be waiting for the improvement.

Strange behavior is also happening that I couldn't understand how to solve and I don't know if it's related to the initial problem.

After the file is rendered on screen, if I save the project, causing hot reload to run, the app is giving fatal error.

DEBUG CONSOLE

Launching lib\main.dart on Mi A1 in debug mode...
Built build\app\outputs\apk\debug\app-debug.apk.
D/com.shockwave.pdfium.PdfiumCore( 7527): Starting PdfiumAndroid 1.9.0
W/us.sommusvenda( 7527): Accessing hidden field Ljava/io/FileDescriptor;->descriptor:I (light greylist, reflection)
D/jniPdfium( 7527): Init FPDF library
E/flutter ( 7527): [ERROR:flutter/shell/platform/android/platform_view_android_jni.cc(39)] java.lang.AbstractMethodError: abstract method "void io.flutter.plugin.platform.PlatformView.onInputConnectionLocked()"
E/flutter ( 7527):  at io.flutter.plugin.platform.VirtualDisplayController.onInputConnectionLocked(VirtualDisplayController.java:166)
E/flutter ( 7527):  at io.flutter.plugin.platform.PlatformViewsController.lockInputConnection(PlatformViewsController.java:370)
E/flutter ( 7527):  at io.flutter.plugin.platform.PlatformViewsController.access$1000(PlatformViewsController.java:36)
E/flutter ( 7527):  at io.flutter.plugin.platform.PlatformViewsController$1.resizePlatformView(PlatformViewsController.java:165)
E/flutter ( 7527):  at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.resize(PlatformViewsChannel.java:120)
E/flutter ( 7527):  at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.onMethodCall(PlatformViewsChannel.java:57)
E/flutter ( 7527):  at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
E/flutter ( 7527):  at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
E/flutter ( 7527):  at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:656)
E/flutter ( 7527):  at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter ( 7527):  at android.os.MessageQueue.next(MessageQueue.java:326)
E/flutter ( 7527):  at android.os.Looper.loop(Looper.java:160)
E/flutter ( 7527):  at android.app.ActivityThread.main(ActivityThread.java:6762)
E/flutter ( 7527):  at java.lang.reflect.Method.invoke(Native Method)
E/flutter ( 7527):  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
E/flutter ( 7527):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
E/flutter ( 7527):
F/flutter ( 7527): [FATAL:flutter/shell/platform/android/platform_view_android_jni.cc(76)] Check failed: CheckException(env).
F/libc    ( 7527): Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 7527 (us.sommusvendas), pid 7527 (us.sommusvendas)
*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
Build fingerprint: 'xiaomi/tissot/tissot_sprout:9/PKQ1.180917.001/V10.0.13.0.PDHMIXM:user/release-keys'
Revision: '0'
ABI: 'arm64'
pid: 7527, tid: 7527, name: us.sommusvendas  >>> com.sommus.sommusvendas <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Abort message: '[FATAL:flutter/shell/platform/android/platform_view_android_jni.cc(76)] Check failed: CheckException(env).
'
    x0  0000000000000000  x1  0000000000001d67  x2  0000000000000006  x3  0000000000000008
    x4  0000000000000000  x5  0000000000000000  x6  0000000000000000  x7  0000000000000080
    x8  0000000000000083  x9  225b81b0efc68628  x10 0000000000000000  x11 fffffffc7ffffbdf
    x12 0000000000000001  x13 0000000000000002  x14 ffffffffffffffff  x15 0000000000000000
    x16 0000007e0bf512b0  x17 0000007e0be729d8  x18 0000000000000010  x19 0000000000001d67
    x20 0000000000001d67  x21 0000007ffe0fa1f8  x22 0000007d6567c070  x23 0000007d6f1eaf58
    x24 000000000000002b  x25 0000007ffe0fa4a0  x26 0000007e1137c5e0  x27 000000000000002b
    x28 0000000000000001  x29 0000007ffe0fa1e0
    sp  0000007ffe0fa1a0  lr  0000007e0be6400c  pc  0000007e0be64034
Reloaded 12 of 567 libraries in 1.826ms.
backtrace:
    #00 pc 0000000000022034  /system/lib64/libc.so (abort+116)
    #01 pc 00000000011d4c3c  /data/app/com.sommus.sommusvendas-x4N4MOuqYqbsukwnexkv5w==/lib/arm64/libflutter.so (offset 0x11c0000)
    #02 pc 00000000011c6f9c  /data/app/com.sommus.sommusvendas-x4N4MOuqYqbsukwnexkv5w==/lib/arm64/libflutter.so (offset 0x11c0000)
    #03 pc 00000000011c549c  /data/app/com.sommus.sommusvendas-x4N4MOuqYqbsukwnexkv5w==/lib/arm64/libflutter.so (offset 0x11c0000)
    #04 pc 000000000120d9ac  /data/app/com.sommus.sommusvendas-x4N4MOuqYqbsukwnexkv5w==/lib/arm64/libflutter.so (offset 0x11c0000)
    #05 pc 00000000011d55a8  /data/app/com.sommus.sommusvendas-x4N4MOuqYqbsukwnexkv5w==/lib/arm64/libflutter.so (offset 0x11c0000)
    #06 pc 00000000011da7c8  /data/app/com.sommus.sommusvendas-x4N4MOuqYqbsukwnexkv5w==/lib/arm64/libflutter.so (offset 0x11c0000)
    #07 pc 0000000000014ff8  /system/lib64/libutils.so (android::Looper::pollInner(int)+836)
    #08 pc 0000000000014c18  /system/lib64/libutils.so (android::Looper::pollOnce(int, int*, int*, void**)+60)
    #09 pc 0000000000126964  /system/lib64/libandroid_runtime.so (android::android_os_MessageQueue_nativePollOnce(_JNIEnv*, _jobject*, long, int)+44)
    #10 pc 0000000000c86a8c  /system/framework/arm64/boot-framework.oat (offset 0x915000) (android.media.MediaExtractor.seekTo [DEDUPED]+140)
    #11 pc 0000000000002a5c  /dev/ashmem/dalvik-jit-code-cache (deleted) (android.os.MessageQueue.next+204)
    #12 pc 0000000000554f88  /system/lib64/libart.so (art_quick_invoke_stub+584)
    #13 pc 00000000000cf6c8  /system/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+200)
    #14 pc 000000000027f22c  /system/lib64/libart.so (art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, art::ShadowFrame*, unsigned short, art::JValue*)+344)
    #15 pc 0000000000279240  /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+968)
    #16 pc 00000000005246f4  /system/lib64/libart.so (MterpInvokeVirtual+588)
    #17 pc 0000000000547694  /system/lib64/libart.so (ExecuteMterpImpl+14228)
    #18 pc 0000000000b22e4c  /system/framework/boot-framework.vdex (android.os.Looper.loop+128)
    #19 pc 0000000000252f44  /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.1997279576+488)
    #20 pc 0000000000258a38  /system/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame*, art::JValue*)+216)
    #21 pc 0000000000279224  /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+940)
    #22 pc 0000000000525bf8  /system/lib64/libart.so (MterpInvokeStatic+204)
    #23 pc 0000000000547814  /system/lib64/libart.so (ExecuteMterpImpl+14612)
    #24 pc 00000000003a2d96  /system/framework/boot-framework.vdex (android.app.ActivityThread.main+214)
    #25 pc 0000000000252f44  /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.1997279576+488)
    #26 pc 0000000000514fa8  /system/lib64/libart.so (artQuickToInterpreterBridge+1020)
    #27 pc 000000000055e0fc  /system/lib64/libart.so (art_quick_to_interpreter_bridge+92)
    #28 pc 000000000055524c  /system/lib64/libart.so (art_quick_invoke_static_stub+604)
    #29 pc 00000000000cf6e8  /system/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+232)
    #30 pc 000000000045c85c  /system/lib64/libart.so (art::(anonymous namespace)::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod*, art::(anonymous namespace)::ArgArray*, art::JValue*, char const*)+104)
    #31 pc 000000000045e2b0  /system/lib64/libart.so (art::InvokeMethod(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jobject*, _jobject*, unsigned long)+1440)
    #32 pc 00000000003ee18c  /system/lib64/libart.so (art::Method_invoke(_JNIEnv*, _jobject*, _jobject*, _jobjectArray*)+52)
    #33 pc 000000000078eed4  /system/framework/arm64/boot-core-oj.oat (offset 0x2dc000) (java.lang.Class.getDeclaredMethodInternal [DEDUPED]+180)
    #34 pc 0000000000554f88  /system/lib64/libart.so (art_quick_invoke_stub+584)
    #35 pc 00000000000cf6c8  /system/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+200)
    #36 pc 000000000027f22c  /system/lib64/libart.so (art::interpreter::ArtInterpreterToCompiledCodeBridge(art::Thread*, art::ArtMethod*, art::ShadowFrame*, unsigned short, art::JValue*)+344)
    #37 pc 0000000000279240  /system/lib64/libart.so (bool art::interpreter::DoCall<false, false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, art::JValue*)+968)
    #38 pc 00000000005246f4  /system/lib64/libart.so (MterpInvokeVirtual+588)
    #39 pc 0000000000547694  /system/lib64/libart.so (ExecuteMterpImpl+14228)
    #40 pc 0000000000dca492  /system/framework/boot-framework.vdex (com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run+22)
    #41 pc 0000000000252f44  /system/lib64/libart.so (_ZN3art11interpreterL7ExecuteEPNS_6ThreadERKNS_20CodeItemDataAccessorERNS_11ShadowFrameENS_6JValueEb.llvm.1997279576+488)
    #42 pc 0000000000514fa8  /system/lib64/libart.so (artQuickToInterpreterBridge+1020)
    #43 pc 000000000055e0fc  /system/lib64/libart.so (art_quick_to_interpreter_bridge+92)
    #44 pc 0000000001a4f2a0  /system/framework/arm64/boot-framework.oat (offset 0x915000) (com.android.internal.os.ZygoteInit.main+3088)
    #45 pc 000000000055524c  /system/lib64/libart.so (art_quick_invoke_static_stub+604)
    #46 pc 00000000000cf6e8  /system/lib64/libart.so (art::ArtMethod::Invoke(art::Thread*, unsigned int*, unsigned int, art::JValue*, char const*)+232)
    #47 pc 000000000045c85c  /system/lib64/libart.so (art::(anonymous namespace)::InvokeWithArgArray(art::ScopedObjectAccessAlreadyRunnable const&, art::ArtMethod*, art::(anonymous namespace)::ArgArray*, art::JValue*, char const*)+104)
    #48 pc 000000000045c4bc  /system/lib64/libart.so (art::InvokeWithVarArgs(art::ScopedObjectAccessAlreadyRunnable const&, _jobject*, _jmethodID*, std::__va_list)+424)
    #49 pc 0000000000361ac8  /system/lib64/libart.so (art::JNI::CallStaticVoidMethodV(_JNIEnv*, _jclass*, _jmethodID*, std::__va_list)+652)
    #50 pc 00000000000b1fa0  /system/lib64/libandroid_runtime.so (_JNIEnv::CallStaticVoidMethod(_jclass*, _jmethodID*, ...)+116)
    #51 pc 00000000000b49c4  /system/lib64/libandroid_runtime.so (android::AndroidRuntime::start(char const*, android::Vector<android::String8> const&, bool)+752)
    #52 pc 000000000000251c  /system/bin/app_process64 (main+2000)
    #53 pc 00000000000ca47c  /system/lib64/libc.so (__libc_init+88)
Lost connection to device.
Exited (sigterm)

This issue is affecting a process I have on a screen that comes after viewing the PDF. I want to email the file and ask on another screen for the user to type. As soon as I click on TextFormField, the above behavior happens.

If you know a way this problem does not occur please let me know.

myselfuser1 commented 1 year ago

This will help https://www.youtube.com/watch?v=gAUVz0U7eyA