Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception while saving file Reply already submitted #14

Closed
pymq opened this issue Sep 3, 2021 · 14 comments
Closed

Exception while saving file Reply already submitted #14

pymq opened this issue Sep 3, 2021 · 14 comments

Comments

@pymq
Copy link

pymq commented Sep 3, 2021

Hi, thanks for this package!

I have 2 functions: one for saving file (export) and another for loading file (import). Loading files is implemented with file_picker library. If I call import many times I have no errors, the same with export. But if I call import and then export (and vise-versa), app crashes (see logs below).

I have zero knowledge of android development, all I can say from logs is that result.success on android side is called twice, probably because of not closed activity (?).

I also tried with flutter_file_dialog for importing, still crashes at file_saver level.

Code samples:

Future<void> _exportSettings() async {
  final exportedSettings = Uint8List.fromList(utf8.encode('{"hello":"world"}'));
  try {
    String response;
    if (kIsWeb) {
      response = await FileSaver.instance.saveFile("config", exportedSettings, "json", mimeType: MimeType.JSON);
    } else {
      response = await FileSaver.instance.saveAs("config.json", exportedSettings, "json", MimeType.JSON);
    }

    print("saved file '$response'");
    return;
  } on Exception catch (e) {
    return print("Failed to save config: ${e.toString()}");
  }
}

Future<void> _importSettings() async {
  try {
    FilePickerResult? result;
    if (kIsWeb) {
      result =
          await FilePicker.platform.pickFiles(type: FileType.custom, allowedExtensions: ["json"], withData: true);
    } else {
      result = await FilePicker.platform.pickFiles(type: FileType.any, withData: true);
    }

    if (result == null) {
      print("aborted importing");
      return;
    }

    final fileBytes = result.files.first.bytes;
    final fileString = utf8.decode(fileBytes!.toList());
    final fileName = result.files.first.name;

    print("imported file '$fileName':");
    print(fileString);
    return;
  } on Exception catch (e) {
    return print("Failed to import config file: ${e.toString()}");
  }
}
Crash log when first saving and then loading file
09-03 19:41:15.160 17784 17784 D FileSaver: Dialog was null
09-03 19:41:15.160 17784 17784 D FileSaver: Creating File Dialog Activity
09-03 19:41:15.160 17784 17784 D FileSaver: Save as Method Called
09-03 19:41:15.161 17784 17784 D Dialog Activity: Opening File Manager
09-03 19:41:15.177 17784 17903 I flutter : didChangeAppLifecycleState AppLifecycleState.inactive
09-03 19:41:15.193 17784 17904 D skia    : Shader compilation error
09-03 19:41:15.193 17784 17904 D skia    : ------------------------
09-03 19:41:15.193 17784 17904 D skia    : Errors:
09-03 19:41:15.193 17784 17904 D skia    : 
09-03 19:41:15.312 17784 17821 D EGL_emulation: eglMakeCurrent: 0x75ba3063d9a0: ver 3 1 (tinfo 0x75ba306cb6c0)
09-03 19:41:15.679 17784 17903 I flutter : didChangeAppLifecycleState AppLifecycleState.paused
09-03 19:41:20.658 17784 17784 D Dialog Activity: Starting file operation
09-03 19:41:20.666 17784 17784 D Dialog Activity: Saving file
09-03 19:41:20.668 17784 17903 I flutter : didChangeAppLifecycleState AppLifecycleState.resumed
09-03 19:41:20.688 17784 17784 D Dialog Activity: Something went wrong
09-03 19:41:20.699 17784 17790 I zygote64: Do partial code cache collection, code=61KB, data=59KB
09-03 19:41:20.699 17784 17790 I zygote64: After code cache collection, code=61KB, data=59KB
09-03 19:41:20.699 17784 17790 I zygote64: Increasing code cache capacity to 256KB
09-03 19:41:20.729 17784 17904 D EGL_emulation: eglMakeCurrent: 0x75ba3859b5e0: ver 3 1 (tinfo 0x75ba2d869ec0)
09-03 19:41:20.733 17784 17904 D EGL_emulation: eglMakeCurrent: 0x75ba3859b5e0: ver 3 1 (tinfo 0x75ba2d869ec0)
09-03 19:41:20.740 17784 17821 D EGL_emulation: eglMakeCurrent: 0x75ba3063d9a0: ver 3 1 (tinfo 0x75ba306cb6c0)


09-03 19:41:32.312 17784 17784 D FilePickerDelegate: Selected type */*
09-03 19:41:32.327 17784 17903 I flutter : didChangeAppLifecycleState AppLifecycleState.inactive
09-03 19:41:32.401 17784 17904 D skia    : Shader compilation error
09-03 19:41:32.401 17784 17904 D skia    : ------------------------
09-03 19:41:32.401 17784 17904 D skia    : Errors:
09-03 19:41:32.401 17784 17904 D skia    : 
09-03 19:41:32.463 17784 17904 D skia    : Shader compilation error
09-03 19:41:32.463 17784 17904 D skia    : ------------------------
09-03 19:41:32.463 17784 17904 D skia    : Errors:
09-03 19:41:32.463 17784 17904 D skia    : 
09-03 19:41:32.478 17784 17821 D EGL_emulation: eglMakeCurrent: 0x75ba3063d9a0: ver 3 1 (tinfo 0x75ba306cb6c0)
09-03 19:41:32.834 17784 17903 I flutter : didChangeAppLifecycleState AppLifecycleState.paused
09-03 19:41:44.897 17784 17784 D Dialog Activity: Starting file operation
09-03 19:41:44.897 17784 17948 I FilePickerUtils: Caching from URI: content://com.android.providers.downloads.documents/document/14
09-03 19:41:44.901 17784 17903 I flutter : didChangeAppLifecycleState AppLifecycleState.resumed
09-03 19:41:44.901 17784 17784 D Dialog Activity: Saving file
09-03 19:41:44.909 17784 17784 D Dialog Activity: Error while writing filePermission Denial: writing com.android.providers.downloads.DownloadStorageProvider uri content://com.android.providers.downloads.documents/document/14 from pid=17784, uid=10067 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
09-03 19:41:44.914 17784 17784 D Dialog Activity: Exception while saving fileReply already submitted
09-03 19:41:44.915 17784 17784 D Dialog Activity: Something went wrong
09-03 19:41:44.920 17784 17948 D FilePickerUtils: File loaded and cached at:/data/user/0/com.anywherelan.awl/cache/file_picker/config_awl.json
09-03 19:41:44.921 17784 17948 D FilePickerDelegate: File path:[com.mr.flutter.plugin.filepicker.FileInfo@b84494b]
--------- beginning of crash
09-03 19:41:44.970 17784 17784 E AndroidRuntime: FATAL EXCEPTION: main
09-03 19:41:44.970 17784 17784 E AndroidRuntime: Process: com.anywherelan.awl, PID: 17784
09-03 19:41:44.970 17784 17784 E AndroidRuntime: java.lang.IllegalStateException: Reply already submitted
09-03 19:41:44.970 17784 17784 E AndroidRuntime:        at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:164)
09-03 19:41:44.970 17784 17784 E AndroidRuntime:        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.error(MethodChannel.java:243)
09-03 19:41:44.970 17784 17784 E AndroidRuntime:        at com.one.file_saver.Dialog$completeFileOperation$1.invokeSuspend(Dialog.kt:72)
09-03 19:41:44.970 17784 17784 E AndroidRuntime:        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
09-03 19:41:44.970 17784 17784 E AndroidRuntime:        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
09-03 19:41:44.970 17784 17784 E AndroidRuntime:        at android.os.Handler.handleCallback(Handler.java:790)
09-03 19:41:44.970 17784 17784 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 19:41:44.970 17784 17784 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:164)
09-03 19:41:44.970 17784 17784 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6494)
09-03 19:41:44.970 17784 17784 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
09-03 19:41:44.970 17784 17784 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
09-03 19:41:44.970 17784 17784 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Crash log when first loading and then saving file
09-03 19:44:10.620 17966 17966 D FilePickerDelegate: Selected type */*
09-03 19:44:10.628 17966 17986 I flutter : didChangeAppLifecycleState AppLifecycleState.inactive
09-03 19:44:10.677 17966 17987 D skia    : Shader compilation error
09-03 19:44:10.677 17966 17987 D skia    : ------------------------
09-03 19:44:10.677 17966 17987 D skia    : Errors:
09-03 19:44:10.677 17966 17987 D skia    : 
09-03 19:44:10.708 17966 17987 D skia    : Shader compilation error
09-03 19:44:10.708 17966 17987 D skia    : ------------------------
09-03 19:44:10.708 17966 17987 D skia    : Errors:
09-03 19:44:10.708 17966 17987 D skia    : 
09-03 19:44:10.781 17966 17997 D EGL_emulation: eglMakeCurrent: 0x75ba306304a0: ver 3 1 (tinfo 0x75ba306fe5c0)
09-03 19:44:11.133 17966 17986 I flutter : didChangeAppLifecycleState AppLifecycleState.paused
09-03 19:44:14.422 17966 18032 I FilePickerUtils: Caching from URI: content://com.android.providers.downloads.documents/document/32
09-03 19:44:14.430 17966 17986 I flutter : didChangeAppLifecycleState AppLifecycleState.resumed
09-03 19:44:14.441 17966 18032 D FilePickerUtils: File loaded and cached at:/data/user/0/com.anywherelan.awl/cache/file_picker/config_awlddddd.json
09-03 19:44:14.441 17966 18032 D FilePickerDelegate: File path:[com.mr.flutter.plugin.filepicker.FileInfo@5f61b44]
09-03 19:44:14.493 17966 17987 D EGL_emulation: eglMakeCurrent: 0x75ba2d862560: ver 3 1 (tinfo 0x75ba2d990620)
09-03 19:44:14.494 17966 17987 D EGL_emulation: eglMakeCurrent: 0x75ba2d862560: ver 3 1 (tinfo 0x75ba2d990620)


09-03 19:44:21.403 17966 17966 D FileSaver: Dialog was null
09-03 19:44:21.403 17966 17966 D FileSaver: Creating File Dialog Activity
09-03 19:44:21.403 17966 17966 D FileSaver: Save as Method Called
09-03 19:44:21.403 17966 17966 D Dialog Activity: Opening File Manager
09-03 19:44:21.410 17966 17986 I flutter : didChangeAppLifecycleState AppLifecycleState.inactive
09-03 19:44:21.572 17966 17997 D EGL_emulation: eglMakeCurrent: 0x75ba306304a0: ver 3 1 (tinfo 0x75ba306fe5c0)
09-03 19:44:21.902 17966 17986 I flutter : didChangeAppLifecycleState AppLifecycleState.paused
09-03 19:44:28.114 17966 17966 D Dialog Activity: Starting file operation
09-03 19:44:28.120 17966 17966 D Dialog Activity: Saving file
09-03 19:44:28.121 17966 17986 I flutter : didChangeAppLifecycleState AppLifecycleState.resumed
09-03 19:44:28.130 17966 17966 D Dialog Activity: Something went wrong
09-03 19:44:28.161 17966 17987 D EGL_emulation: eglMakeCurrent: 0x75ba2d862560: ver 3 1 (tinfo 0x75ba2d990620)
09-03 19:44:28.163 17966 17987 D EGL_emulation: eglMakeCurrent: 0x75ba2d862560: ver 3 1 (tinfo 0x75ba2d990620)
09-03 19:44:28.180 17966 17997 D EGL_emulation: eglMakeCurrent: 0x75ba306304a0: ver 3 1 (tinfo 0x75ba306fe5c0)
09-03 19:44:29.772 17966 18008 I GoLog   : 2021-09-03 16:44:29  INFO    dht     processed event event.EvtLocalReachabilityChanged; performing dht mode switch
09-03 19:44:29.772 17966 18008 I GoLog   : 2021-09-03 16:44:29  INFO    dht     switched DHT mode successfully  {"mode": 2}
09-03 19:44:33.164 17966 17966 D FileSaver: Save as Method Called
09-03 19:44:33.164 17966 17966 D Dialog Activity: Opening File Manager
09-03 19:44:33.191 17966 17986 I flutter : didChangeAppLifecycleState AppLifecycleState.inactive
09-03 19:44:33.293 17966 17997 D EGL_emulation: eglMakeCurrent: 0x75ba306304a0: ver 3 1 (tinfo 0x75ba306fe5c0)
09-03 19:44:33.660 17966 17986 I flutter : didChangeAppLifecycleState AppLifecycleState.paused
09-03 19:44:38.755 17966 17966 D Dialog Activity: Starting file operation
09-03 19:44:38.760 17966 17966 D Dialog Activity: Saving file
09-03 19:44:38.761 17966 17986 I flutter : didChangeAppLifecycleState AppLifecycleState.resumed
09-03 19:44:38.773 17966 17966 D Dialog Activity: Something went wrong
09-03 19:44:38.812 17966 17987 D EGL_emulation: eglMakeCurrent: 0x75ba2d862560: ver 3 1 (tinfo 0x75ba2d990620)
09-03 19:44:38.816 17966 17987 D EGL_emulation: eglMakeCurrent: 0x75ba2d862560: ver 3 1 (tinfo 0x75ba2d990620)
09-03 19:44:38.843 17966 17997 D EGL_emulation: eglMakeCurrent: 0x75ba306304a0: ver 3 1 (tinfo 0x75ba306fe5c0)
09-03 19:44:41.576 17966 17966 D FilePickerDelegate: Selected type */*
09-03 19:44:41.592 17966 17986 I flutter : didChangeAppLifecycleState AppLifecycleState.inactive
09-03 19:44:41.714 17966 17997 D EGL_emulation: eglMakeCurrent: 0x75ba306304a0: ver 3 1 (tinfo 0x75ba306fe5c0)
09-03 19:44:42.086 17966 17986 I flutter : didChangeAppLifecycleState AppLifecycleState.paused


09-03 19:46:02.914 17966 17966 D Dialog Activity: Starting file operation
09-03 19:46:02.915 17966 18071 I FilePickerUtils: Caching from URI: content://com.android.providers.downloads.documents/document/14
09-03 19:46:02.925 17966 17966 D Dialog Activity: Saving file
09-03 19:46:02.926 17966 17986 I flutter : didChangeAppLifecycleState AppLifecycleState.resumed
09-03 19:46:02.927 17966 17966 D Dialog Activity: Error while writing filePermission Denial: writing com.android.providers.downloads.DownloadStorageProvider uri content://com.android.providers.downloads.documents/document/14 from pid=17966, uid=10067 requires android.permission.MANAGE_DOCUMENTS, or grantUriPermission()
09-03 19:46:02.935 17966 18071 D FilePickerUtils: File loaded and cached at:/data/user/0/com.anywherelan.awl/cache/file_picker/config_awl.json
09-03 19:46:02.936 17966 18071 D FilePickerDelegate: File path:[com.mr.flutter.plugin.filepicker.FileInfo@339d486]
09-03 19:46:02.937 17966 17966 D Dialog Activity: Exception while saving fileReply already submitted
09-03 19:46:02.937 17966 17966 D Dialog Activity: Something went wrong
09-03 19:46:02.954 17966 17971 I zygote64: Do partial code cache collection, code=61KB, data=56KB
09-03 19:46:02.955 17966 17971 I zygote64: After code cache collection, code=61KB, data=56KB
09-03 19:46:02.955 17966 17971 I zygote64: Increasing code cache capacity to 256KB
--------- beginning of crash
09-03 19:46:02.992 17966 17966 E AndroidRuntime: FATAL EXCEPTION: main
09-03 19:46:02.992 17966 17966 E AndroidRuntime: Process: com.anywherelan.awl, PID: 17966
09-03 19:46:02.992 17966 17966 E AndroidRuntime: java.lang.IllegalStateException: Reply already submitted
09-03 19:46:02.992 17966 17966 E AndroidRuntime:        at io.flutter.embedding.engine.dart.DartMessenger$Reply.reply(DartMessenger.java:164)
09-03 19:46:02.992 17966 17966 E AndroidRuntime:        at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler$1.error(MethodChannel.java:243)
09-03 19:46:02.992 17966 17966 E AndroidRuntime:        at com.one.file_saver.Dialog$completeFileOperation$1.invokeSuspend(Dialog.kt:72)
09-03 19:46:02.992 17966 17966 E AndroidRuntime:        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
09-03 19:46:02.992 17966 17966 E AndroidRuntime:        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
09-03 19:46:02.992 17966 17966 E AndroidRuntime:        at android.os.Handler.handleCallback(Handler.java:790)
09-03 19:46:02.992 17966 17966 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 19:46:02.992 17966 17966 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:164)
09-03 19:46:02.992 17966 17966 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6494)
09-03 19:46:02.992 17966 17966 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
09-03 19:46:02.992 17966 17966 E AndroidRuntime:        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
09-03 19:46:02.992 17966 17966 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Reproduced with android 8.1 and 11, file_saver library version b0b83c6 and 0.0.10.

@incrediblezayed
Copy link
Owner

Okay, I will look into it.

@pymq
Copy link
Author

pymq commented Oct 29, 2021

Looking forward to this!

@AmanuelYosief
Copy link

Same, just experienced this issue, while testing mobile application. I think this is the only package I found that makes it easy to store fines. @pymq Do you know any others/alternatives.

The issue is related to a file and saving it, then proceeding to upload another file. Perhaps, it hasn't fully completed the first operation of saving, or perhaps, it's related to the cache and hasn't been cleaned after it's completed.

There should be no link between first operation and any other future ones.

@pymq
Copy link
Author

pymq commented Oct 30, 2021

@AmanuelYosief Unfortunately, no, I'm still facing this issue. Postponed this for a while. I tried with other libraries, but they conflict somehow.

@incrediblezayed
Copy link
Owner

Can we connect on reddit or some direct chat application? @pymq @AmanuelYosief

@Iucone
Copy link

Iucone commented Feb 10, 2022

I get the same exception (on Android) using the saveAs method and than importing a new file using image_picker plugin. There should be a check in the method onActivityResult (Dialog.kt) to see if the requestCode is the correct one.

@incrediblezayed
Copy link
Owner

direct

Can you share a piece of your code so I can reproduce the issue, also, can you please elaborate on the part "if the requestCode is correct one" since in not a very experienced native developer, I'm just putting my hands into native part

@stonega
Copy link

stonega commented Feb 14, 2022

@pymq @incrediblezayed
Seems like dispose dialog after saving file could fix the issue.

"dispose" -> {
   Log.d(tag, "Dispose Activity")
    if (dialog != null) {
        activity?.removeActivityResultListener(dialog!!)
        dialog = null;
    }
 }

@Iucone
Copy link

Iucone commented Feb 15, 2022

direct

Can you share a piece of your code so I can reproduce the issue, also, can you please elaborate on the part "if the requestCode is correct one" since in not a very experienced native developer, I'm just putting my hands into native part

I just added a check to see if requestCode is equal to SAVE_FILE in the onActivityResult() method:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
        if (requestCode == SAVE_FILE && resultCode == Activity.RESULT_OK && data?.data != null) {
            Log.d(TAG, "Starting file operation")
            completeFileOperation(data.data!!)
        } else {
            Log.d(TAG, "Activity result was null")
            return false
        }
        return true
    }

@AmanuelYosief
Copy link

@pymq @incrediblezayed Seems like dispose dialog after saving file could fix the issue.

"dispose" -> {
   Log.d(tag, "Dispose Activity")
    if (dialog != null) {
        activity?.removeActivityResultListener(dialog!!)
        dialog = null;
    }
 }

if this works, where would I include it?, how does it work? and does it only affect the file_saver packages.

@incrediblezayed
Copy link
Owner

Hi, I'll fix the issue and merge it by tomorrow, please wait till then if you can, Thank You

incrediblezayed added a commit that referenced this issue Feb 20, 2022
 * Fixed the incomplete path in saveFile method #16
 * Fixed some crashes in some folders on saveAs method
 * Fixed application crash issue **_reply already submitted_** #14
* Fixed **_Wrong or missing file extension when calling saveAs()_** Issue #20
@incrediblezayed
Copy link
Owner

incrediblezayed commented Feb 20, 2022

The issue is fixed, thank you all so much, especially @Iucone, whose trick worked and everything seems fine now, use version 0.1.0

@pymq
Copy link
Author

pymq commented Feb 25, 2022

I tested the new version, it worked well for me, thank you all!

@AmanuelYosief
Copy link

Worked, this required updating to version 0.1.0
also updating Kotlin version: 1.60
gradle version: 6.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
@stonega @Iucone @incrediblezayed @pymq @AmanuelYosief and others