From 49978f4b43d2a85d47ca7ac682e16564f285bc77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reimar=20D=C3=B6ffinger?= Date: Tue, 21 Jul 2020 19:57:45 +0200 Subject: [PATCH] Fix native file descriptor being closed while still exporting. When the ParcelFileDescriptor gets garbage collected that will close the associated file descriptor. Since the exporting happens async in a different block, we can end up trying to write to a closed file descriptor. To fix this, just pass the ParcelFileDescriptor to the object doing the async export, so it will be destroyed only after we have closed the stream we write to. Fixes issue #188. --- .../kolabnotes/android/fragment/OverviewFragment.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/org/kore/kolabnotes/android/fragment/OverviewFragment.java b/app/src/main/java/org/kore/kolabnotes/android/fragment/OverviewFragment.java index 85b6e7f..758d721 100644 --- a/app/src/main/java/org/kore/kolabnotes/android/fragment/OverviewFragment.java +++ b/app/src/main/java/org/kore/kolabnotes/android/fragment/OverviewFragment.java @@ -1196,9 +1196,8 @@ public void onActivityResult(int requestCode, int resultCode, } ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "w"); - FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor()); - new ExportNotebook(getActivity(), uri, fileOutputStream).execute(activeAccount.getAccount(), activeAccount.getRootFolder(), notebookName); + new ExportNotebook(getActivity(), uri, pfd).execute(activeAccount.getAccount(), activeAccount.getRootFolder(), notebookName); }catch (FileNotFoundException e){ Log.e("result", e.getMessage(), e); NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE); @@ -1331,13 +1330,13 @@ private void exportNotebooks(){ class ExportNotebook extends AsyncTask{ private final Context context; - private final OutputStream pathToZIP; + private final ParcelFileDescriptor pfd; private final Uri fileUri; private final Random random; - ExportNotebook(Context context, Uri fileUri, OutputStream pathToZIP){ + ExportNotebook(Context context, Uri fileUri, ParcelFileDescriptor pfd){ this.context = context; - this.pathToZIP = pathToZIP; + this.pfd = pfd; random = new Random(); this.fileUri = fileUri; } @@ -1347,6 +1346,7 @@ class ExportNotebook extends AsyncTask{ protected String doInBackground(String... params) { try { Log.d("export", Arrays.toString(params)); + FileOutputStream pathToZIP = new FileOutputStream(pfd.getFileDescriptor()); Notebook notebook = notebookRepository.getBySummary(params[0], params[1], params[2]); List fromNotebook = notesRepository.getFromNotebookWithDescriptionLoaded(params[0], params[1], notebook.getIdentification().getUid(), new NoteSorting()); @@ -1358,6 +1358,7 @@ protected String doInBackground(String... params) { repository.exportNotebook(notebook, new KolabNotesParserV3(), pathToZIP); + pathToZIP.close(); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); intent.setData(fileUri);