miguelpruivo/flutter_file_picker

[Android] app crashes when using `getDirectoryPath()` or `saveFile()`

Closed this issue · 4 comments

Describe the bug
When calling either getDirectoryPath() or saveFile() on Android the app crashes and only some logs are shown on the console.

Platform

  • Android
  • iOS
  • Web
  • Desktop

Platform OS version
Different Android phones and emulators

How are you picking?

String? path = await FilePicker.platform.getDirectoryPath();

and

String? path = await FilePicker.platform.saveFile();

Details to reproduce the issue
Just call the methods mentioned above.

Error Log

E/AndroidRuntime(11787): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=36888, result=-1, data=Intent { dat=content://com.android.externalstorage.documents/... flg=0xc3 }} to activity {com.DaAppLab.DaKanjiRecognizer/io.flutter.embedding.android.FlutterActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
E/AndroidRuntime(11787): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:5323)
E/AndroidRuntime(11787): 	at android.app.ActivityThread.handleSendResult(ActivityThread.java:5362)
E/AndroidRuntime(11787): 	at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:67)
E/AndroidRuntime(11787): 	at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
E/AndroidRuntime(11787): 	at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
E/AndroidRuntime(11787): 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
E/AndroidRuntime(11787): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2307)
E/AndroidRuntime(11787): 	at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(11787): 	at android.os.Looper.loopOnce(Looper.java:201)
E/AndroidRuntime(11787): 	at android.os.Looper.loop(Looper.java:288)
E/AndroidRuntime(11787): 	at android.app.ActivityThread.main(ActivityThread.java:7872)
E/AndroidRuntime(11787): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(11787): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/AndroidRuntime(11787): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
E/AndroidRuntime(11787): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
E/AndroidRuntime(11787): 	at com.twelve_ampere.request_permission.RequestPermissionPlugin$2.onActivityResult(RequestPermissionPlugin.java:94)
E/AndroidRuntime(11787): 	at io.flutter.embedding.engine.FlutterEngineConnectionRegistry$FlutterEngineActivityPluginBinding.onActivityResult(FlutterEngineConnectionRegistry.java:774)
E/AndroidRuntime(11787): 	at io.flutter.embedding.engine.FlutterEngineConnectionRegistry.onActivityResult(FlutterEngineConnectionRegistry.java:422)
E/AndroidRuntime(11787): 	at io.flutter.embedding.android.FlutterActivityAndFragmentDelegate.onActivityResult(FlutterActivityAndFragmentDelegate.java:857)
E/AndroidRuntime(11787): 	at io.flutter.embedding.android.FlutterActivity.onActivityResult(FlutterActivity.java:884)
E/AndroidRuntime(11787): 	at android.app.Activity.dispatchActivityResult(Activity.java:8628)
E/AndroidRuntime(11787): 	at android.app.ActivityThread.deliverResults(ActivityThread.java:5316)
E/AndroidRuntime(11787): 	... 13 more
D/FilePickerDelegate(11787): [SingleFilePick] File URI:content://com.android.externalstorage.documents/tree/primary%3ADownload%2FTest/document/primary%3ADownload%2FTest
W/KanjiRecognizer(11787): Accessing hidden method Landroid/os/storage/StorageManager;->getVolumeList()[Landroid/os/storage/StorageVolume; (unsupported, reflection, allowed)
I/Process (11787): Sending signal. PID: 11787 SIG: 9
Lost connection to device.

Exited.

Screenshots and/or video
Watch the video

Flutter Version details

[✓] Flutter (Channel stable, 3.19.5, on macOS 13.5.2 22G91 darwin-arm64, locale en-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.2)
[✓] VS Code (version 1.88.1)
[✓] Connected device (3 available)
    ! Error: Browsing on the local area network for DaAppLab’s iPad. Ensure the device is unlocked and
      attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
[✓] Network resources

• No issues found!

@navaronbracke do you have any idea what I could be doing wrong?

For me, this was related to the usage of the plugin request_permission.

If you look at my error message, you can see that the mentioned plug-in was the culprit

E/AndroidRuntime(11787): at com.twelve_ampere.request_permission.RequestPermissionPlugin$2.onActivityResult(RequestPermissionPlugin.java:94)

Turns out that this plugin overwrites the method onActivityResult in an incorrect way. As the wiki mentions

Note: If your are overriding onActivityResult in your MainActivity, make sure to call super.onActivityResult(...) for unhandled activities. Otherwise picking a file might fail silently.

So, I fixed the issue by removing that plug-in.

Thank you for your answer. I tried to pass the bytes parameter to solve the application crash issue, and it seems that this parameter is necessary.