miguelpruivo / flutter_file_picker

File picker plugin for Flutter, compatible with mobile (iOS & Android), Web, Desktop (Mac, Linux, Windows) platforms with Flutter Go support.
MIT License
1.35k stars 672 forks source link

[Android] .saveFile() returns null array #1588

Closed deathblade666 closed 1 month ago

deathblade666 commented 2 months ago

Before creating an issue, make sure that you are on the latest file_picker version and that there aren't already any similar opened inssues. Also, check if it isn't described on the Wiki, specially on Troubleshooting page.

Also, sometimes a simple flutter clean and flutter build again with latest file_picker version, may end up by fixing cached issues, so I encourage you to first do so.

Describe the bug A clear and concise description of what the bug is. If the issue happens to be on Android, please make sure that it also happens with a different device/simulator and/or version.

On android 15 .saveFile() function returns null array. On Android 14 .saveFile() returns a null array if bytes is not provide (see edit at end of post)

Platform

Platform OS version What version did it happen? 15 beta (august 2024 release) How are you picking?

String? result = FilePicker.platform.saveFile();

also tried

String? result = FilePicker.platform.saveFile(bytes: <bytes>);

Details to reproduce the issue Provide all the details to reproduce the issue.

Add the above code snippet, select a file (or "create" one) press the confirm button

Error Log Please, post the full console log of your issue, if applicable.

E/AndroidRuntime(18309): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=13619, result=-1, data=Intent { dat=content://com.android.externalstorage.documents/... flg=0x43 }} to activity {com.deathblade666.mdeditor/com.deathblade666.mdeditor.MainActivity}: java.lang.NullPointerException: Attempt to get length of null array
E/AndroidRuntime(18309):    at android.app.ActivityThread.deliverResults(ActivityThread.java:5815)
E/AndroidRuntime(18309):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:5854)
E/AndroidRuntime(18309):    at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:69)
E/AndroidRuntime(18309):    at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:60)
E/AndroidRuntime(18309):    at android.app.servertransaction.TransactionExecutor.executeNonLifecycleItem(TransactionExecutor.java:231)
E/AndroidRuntime(18309):    at android.app.servertransaction.TransactionExecutor.executeTransactionItems(TransactionExecutor.java:152)
E/AndroidRuntime(18309):    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:93)
E/AndroidRuntime(18309):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2595)
E/AndroidRuntime(18309):    at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(18309):    at android.os.Looper.loopOnce(Looper.java:232)
E/AndroidRuntime(18309):    at android.os.Looper.loop(Looper.java:317)
E/AndroidRuntime(18309):    at android.app.ActivityThread.main(ActivityThread.java:8592)
E/AndroidRuntime(18309):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(18309):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
E/AndroidRuntime(18309):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
E/AndroidRuntime(18309): Caused by: java.lang.NullPointerException: Attempt to get length of null array

Screenshots and/or video If applicable, add screenshots or video to help explain your problem.

Flutter Version details Please, post the output of your flutter doctor -v, preferably, while running the issued device/simulator.

[✓] Flutter (Channel stable, 3.24.1, on Arch Linux 6.10.4-arch2-1, locale en_US.UTF-8)
    • Flutter version 3.24.1 on channel stable at /usr/bin/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 5874a72aa4 (11 days ago), 2024-08-20 16:46:00 -0500
    • Engine revision c9b9d5780d
    • Dart version 3.5.1
    • DevTools version 2.37.2

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /home/deathmasia/Android/Sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /opt/android-studio/jbr/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)
    • All Android licenses accepted.

[✗] Chrome - develop for the web (Cannot find Chrome executable at google-chrome)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[✓] Linux toolchain - develop for Linux desktop
    • clang version 18.1.8
    • cmake version 3.30.2
    • ninja version 1.12.1
    • pkg-config version 2.1.1

[✓] Android Studio (version 2024.1)
    • Android Studio at /opt/android-studio
    • Flutter plugin version 81.0.2
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.11+0-17.0.11b1207.24-11852314)

[✓] Connected device (2 available)
    • Pixel 2 XL (mobile) • 710KPRW0306414 • android-arm64 • Android 14 (API 34)
    • Linux (desktop)     • linux          • linux-x64     • Arch Linux 6.10.4-arch2-1

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Additional context Add any other context about the problem here.

There similar issue with other functions like the pickFiles() but i can open a separate issue for that if preferred. I'll open another issue for the pickFiles() as while it doesnt work it doesnt behave the same as detailed in this issue.

EDIT: upon further testing, it appears that on android 14 this same issue occurs if bytes is not provided, please consider making it clear in the docs that bytes is required for at least android (unable to test IOS). As it stands the only mention in the docs is "For mobile platforms, this function will save file with bytes to return a path." this does not convey that bytes is required.

nafiskabbo commented 2 months ago

I am also getting same issue in Android 14. If I send bytes then it works. Error:

    String? outputFile = await FilePicker.platform.saveFile(
      dialogTitle: 'Please select an output file:',
      fileName: '$fileName${Constants.fileType}',
    );

Working:

    String? outputFile = await FilePicker.platform.saveFile(
      dialogTitle: 'Please select an output file:',
      fileName: '$fileName${Constants.fileType}',
      bytes: stringToUint8List(fileName),
    );

  Uint8List stringToUint8List(String data) {
    // Convert string to bytes (List<int>)
    List<int> encoded = utf8.encode(data);

    // Convert List<int> to Uint8List
    Uint8List uint8List = Uint8List.fromList(encoded);

    return uint8List;
  }
github-actions[bot] commented 2 months ago

This issue is stale because it has been open for 7 days with no activity.

github-actions[bot] commented 1 month ago

This issue was closed because it has been inactive for 14 days since being marked as stale.