miguelpruivo/flutter_file_picker

App crashes when selecting an image

Closed this issue ยท 13 comments

Describe the bug
When selecting an image from the gallery, while we want to select only one image in Android version 10, after selecting the image, the app crashes and comes out and closes.

Platform

  • Android
  • iOS
  • Web
  • Desktop

Platform OS version
android version 10

How are you picking?

  FilePickerResult? result = await FilePicker.platform.pickFiles(
    type: FileType.image,
  );

**Error Log**
E/AndroidRuntime( 6085): FATAL EXCEPTION: Thread-13
E/AndroidRuntime( 6085): Process: com.example.my_chat_app, PID: 6085
E/AndroidRuntime( 6085): java.lang.RuntimeException: java.io.IOException: Permission denied
E/AndroidRuntime( 6085): 	at com.mr.flutter.plugin.filepicker.FileUtils.compressImage(FileUtils.java:113)
E/AndroidRuntime( 6085): 	at com.mr.flutter.plugin.filepicker.FilePickerDelegate$2.run(FilePickerDelegate.java:119)
E/AndroidRuntime( 6085): 	at java.lang.Thread.run(Thread.java:919)
E/AndroidRuntime( 6085): Caused by: java.io.IOException: Permission denied
E/AndroidRuntime( 6085): 	at java.io.UnixFileSystem.createFileExclusively0(Native Method)
E/AndroidRuntime( 6085): 	at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
E/AndroidRuntime( 6085): 	at java.io.File.createTempFile(File.java:2018)
E/AndroidRuntime( 6085): 	at com.mr.flutter.plugin.filepicker.FileUtils.createImageFile(FileUtils.java:121)
E/AndroidRuntime( 6085): 	at com.mr.flutter.plugin.filepicker.FileUtils.compressImage(FileUtils.java:100)
E/AndroidRuntime( 6085): 	... 2 more
D/ViewRootImpl@fc322ae[MainActivity]( 6085): MSG_WINDOW_FOCUS_CHANGED 1 1
**Screenshots and/or video**
If applicable, add screenshots or video to help explain your problem.

**Flutter Version details**
Flutter 3.16.8

I had the same problem. The only way I found to fix it is to set compressionQuality to 0 in pickFiles() method or return to the version [6.1.1].
There's definitely something wrong with compression on android)

I had the same problem. The only way I found to fix it is to set compressionQuality to 0 in pickFiles() method or return to the version [6.1.1]. There's definitely something wrong with compression on android)

Yes, for me too, when I set the compressionQuality to 0, it works correctly, but it is still clear that there is an issue with other numbers.

I'm having the same problem. The crash happens for some gallery apps, while for some it doesn't happen at all. Using Samsung gallery works, and using Google photos causes a crash. I suspect it has to do with using photos stored locally vs ones stored on the cloud.

file_picker: ^6.2.0

yes, it is crashing when select image file from gallery.

Me, too. Here is my error message:

E/OpenGLRenderer( 5838): Unable to match the desired swap behavior.
E/BitmapFactory( 5838): Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Download/2.png: open failed: EACCES (Permission denied)
E/AndroidRuntime( 5838): FATAL EXCEPTION: Thread-4
E/AndroidRuntime( 5838): Process: com.example.posnote, PID: 5838
E/AndroidRuntime( 5838): java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference

on Android API 34.

Picking videos and audios are fine.

Once again โ€” we are facing custom file pickers differences along with the Android OS fragmentation that can trigger niche issues such as this one. I always recommend to use native Android (non custom branding/OS) picker to test/validate the issue.

Sounds pretty much exactly like this problem #1460, that was just fixed by merging #1458

Great. Closing! Let me know if you still experience it again.

this crash is still occurring. In version 8.0.0+1 and in flutter 3.19.5. I think the issue was closed without fixing(sorry about that). Please let me know if you have any working solutions.

The issue is reproducible only on Android 10 devices, NPE was 14

This issue is not fixed on android 10. It is 100% reproduce on android 10 device. Please reopen this issue.

Exception java.lang.RuntimeException: java.io.IOException: Permission denied
at com.mr.flutter.plugin.filepicker.FileUtils.compressImage (FileUtils.java:111)
at com.mr.flutter.plugin.filepicker.FilePickerDelegate$2.run (FilePickerDelegate.java:152)
at java.lang.Thread.run (Thread.java:919)
Caused by java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively0
at java.io.UnixFileSystem.createFileExclusively (UnixFileSystem.java:317)
at java.io.File.createTempFile (File.java:2018)
at com.mr.flutter.plugin.filepicker.FileUtils.createImageFile (FileUtils.java:120)
at com.mr.flutter.plugin.filepicker.FileUtils.compressImage (FileUtils.java:98)

Hi. I was able to resolve the crash by adding the following in my AndroidManifest.xml

<manifest>
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
   <!-- Your other permissions -->
   <application android:requestLegacyExternalStorage="true">

Important: You also have to request the permission Permission.storage first before you use the plugin. For some reason, letting the plugin handle the permission causes the same crash.

Tested this on an Android 10 emulator.

Hi @dustincatap,
This does not solve it on physical device running Android 9.
First IO Exception issue comes from : file_picker-8.0.1/android/src/main/java/com/mr/flutter/plugin/filepicker/FileUtils.java

private static File createImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        return File.createTempFile(imageFileName, ".jpg", storageDir);
    }

Which is triggered by compressImage.
Then by setting compression to false and compressionQuality: 0 IO Exception error is avoided, but further down the app still crashes.

D/Surface (15609): Surface::disconnect(this=0x7de50a3000,api=1)
cc : @miguelpruivo