From e85c8f4d061c9583ce2fd9e0a473e3dedbfe2ae0 Mon Sep 17 00:00:00 2001 From: ArtieHerasymov Date: Mon, 20 Nov 2023 22:25:49 +0200 Subject: [PATCH] createExternalStoragePrivateFile executing in chunks --- .../ivpusic/imagepicker/PickerModule.java | 35 +++++++++++-------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java b/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java index 5de0845b2..a260dd6a8 100644 --- a/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java +++ b/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java @@ -620,15 +620,18 @@ private File createExternalStoragePrivateFile(Context context, Uri uri) throws F try { // Very simple code to copy a picture from the application's // resource into the external file. Note that this code does - // no error checking, and assumes the picture is small (does not - // try to copy it in chunks). Note that if external storage is + // no error checking. Note that if external storage is // not currently mounted this will silently fail. - OutputStream outputStream = new FileOutputStream(file); - byte[] data = new byte[inputStream.available()]; - inputStream.read(data); - outputStream.write(data); + FileOutputStream fileOutputStream = new FileOutputStream(file); + int bufferSize = 1024; + byte[] buffer = new byte[bufferSize]; + int len; + while ((len = inputStream.read(buffer, 0 , bufferSize)) != -1) { + fileOutputStream.write(buffer, 0, len); + fileOutputStream.flush(); + } inputStream.close(); - outputStream.close(); + fileOutputStream.close(); } catch (IOException e) { // Unable to create file, likely because external storage is // not currently mounted. @@ -873,13 +876,17 @@ private void croppingResult(Activity activity, final int requestCode, final int @Override public void onActivityResult(Activity activity, final int requestCode, final int resultCode, final Intent data) { - if (requestCode == IMAGE_PICKER_REQUEST) { - imagePickerResult(activity, requestCode, resultCode, data); - } else if (requestCode == CAMERA_PICKER_REQUEST) { - cameraPickerResult(activity, requestCode, resultCode, data); - } else if (requestCode == UCrop.REQUEST_CROP) { - croppingResult(activity, requestCode, resultCode, data); - } + Runnable backgroundRunnable = () -> { + if (requestCode == IMAGE_PICKER_REQUEST) { + imagePickerResult(activity, requestCode, resultCode, data); + } else if (requestCode == CAMERA_PICKER_REQUEST) { + cameraPickerResult(activity, requestCode, resultCode, data); + } else if (requestCode == UCrop.REQUEST_CROP) { + croppingResult(activity, requestCode, resultCode, data); + } + }; + + new Thread(backgroundRunnable).start(); } @Override