Open krmao opened 7 months ago
I will try with patch by chatgpt for a test
diff --git a/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java b/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java
index 5de0845..5042117 100644
--- a/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java
+++ b/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java
@@ -599,7 +599,8 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi
&& !path.startsWith(externalFilesDirPath)
&& !path.startsWith(cacheDirPath)
&& !path.startsWith(FilesDirPath)) {
- File copiedFile = this.createExternalStoragePrivateFile(activity, uri);
+ // File copiedFile = this.createExternalStoragePrivateFile(activity, uri);
+ File copiedFile = this.createExternalStoragePrivateFileV2(activity, uri);
path = RealPathUtil.getRealPathFromURI(activity, Uri.fromFile(copiedFile));
}
}
@@ -638,6 +639,50 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi
return file;
}
+ private File createExternalStoragePrivateFileV2(Context context, Uri uri) throws IOException {
+ InputStream inputStream = null;
+ OutputStream outputStream = null;
+ File file = null;
+ try {
+ inputStream = context.getContentResolver().openInputStream(uri);
+ if (inputStream == null) {
+ throw new FileNotFoundException("InputStream is null for URI: " + uri);
+ }
+ String extension = this.getExtension(context, uri);
+ file = new File(context.getExternalCacheDir(), "/temp/" + System.currentTimeMillis() + "." + extension);
+ File parentFile = file.getParentFile();
+ if (parentFile != null) {
+ parentFile.mkdirs();
+ }
+ outputStream = new FileOutputStream(file);
+ byte[] buffer = new byte[8192];
+ int bytesRead;
+ while ((bytesRead = inputStream.read(buffer)) != -1) {
+ outputStream.write(buffer, 0, bytesRead);
+ }
+ } catch (IOException e) {
+ Log.w("image-crop-picker", "Error writing " + file, e);
+ throw e;
+ } finally {
+ if (inputStream != null) {
+ try {
+ inputStream.close();
+ } catch (IOException e) {
+ Log.e("image-crop-picker", "Error closing InputStream", e);
+ }
+ }
+ if (outputStream != null) {
+ try {
+ outputStream.close();
+ } catch (IOException e) {
+ Log.e("image-crop-picker", "Error closing OutputStream", e);
+ }
+ }
+ }
+ return file;
+ }
+
+
public String getExtension(Context context, Uri uri) {
String extension;
and add the code to AndroidManifest.xml
android:largeHeap="true"
android:hardwareAccelerated="true"
It worked!
I will try with patch by chatgpt for a test
diff --git a/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java b/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java index 5de0845..5042117 100644 --- a/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java +++ b/node_modules/react-native-image-crop-picker/android/src/main/java/com/reactnative/ivpusic/imagepicker/PickerModule.java @@ -599,7 +599,8 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi && !path.startsWith(externalFilesDirPath) && !path.startsWith(cacheDirPath) && !path.startsWith(FilesDirPath)) { - File copiedFile = this.createExternalStoragePrivateFile(activity, uri); + // File copiedFile = this.createExternalStoragePrivateFile(activity, uri); + File copiedFile = this.createExternalStoragePrivateFileV2(activity, uri); path = RealPathUtil.getRealPathFromURI(activity, Uri.fromFile(copiedFile)); } } @@ -638,6 +639,50 @@ class PickerModule extends ReactContextBaseJavaModule implements ActivityEventLi return file; } + private File createExternalStoragePrivateFileV2(Context context, Uri uri) throws IOException { + InputStream inputStream = null; + OutputStream outputStream = null; + File file = null; + try { + inputStream = context.getContentResolver().openInputStream(uri); + if (inputStream == null) { + throw new FileNotFoundException("InputStream is null for URI: " + uri); + } + String extension = this.getExtension(context, uri); + file = new File(context.getExternalCacheDir(), "/temp/" + System.currentTimeMillis() + "." + extension); + File parentFile = file.getParentFile(); + if (parentFile != null) { + parentFile.mkdirs(); + } + outputStream = new FileOutputStream(file); + byte[] buffer = new byte[8192]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + } catch (IOException e) { + Log.w("image-crop-picker", "Error writing " + file, e); + throw e; + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + Log.e("image-crop-picker", "Error closing InputStream", e); + } + } + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + Log.e("image-crop-picker", "Error closing OutputStream", e); + } + } + } + return file; + } + + public String getExtension(Context context, Uri uri) { String extension;
and add the code to AndroidManifest.xml
android:largeHeap="true" android:hardwareAccelerated="true"
@krmao I tried and it worked now I'm able to select media greater than 1GB but as i exceed 1.5 Gb I'm getting negative media size and app is getting freeze (App not responding)
Is there anyway in android where I can get the size of media without reading the whole file
@atultiwaree
import { getImageMetaData, getVideoMetaData, Video } from 'react-native-compressor';
const getImageInfo = (filePath: string) => {
return getImageMetaData(filePath).then(result => ({
...result,
size: displayUtil.IS_IOS ? result.size : result.size * 1024, // android 下 size 发现少了 1024倍
}));
};
const getVideoInfo = (filePath: string) => {
return getVideoMetaData(filePath).then(result => ({
...result,
size: displayUtil.IS_IOS ? result.size : result.size * 1024, // android 下 size 发现少了 1024倍
}));
};
or
use RNFS to read file size
getVideoMetaD
Thanks @krmao but to get the file path we have to use react-native-image-crop-picker and that's when the app is getting freeze how do I tackle that is there any native way of doing such things or any library I was using react-native-image-picker but it takes lot of time so I switched to RNICP
Version
Tell us which versions you are using:
Platform
Tell us to which platform this issue is related
Expected behaviour
not crash
Actual behaviour
crash
Steps to reproduce
select big video
crash
Attachments
Sentry crash logs