miguelpruivo/flutter_file_picker

`NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress'`

Closed this issue · 3 comments

Describe the bug

I am getting this crash in file_picker:

03-06 23:31:01.715  1461  1733 W MediaProvider: isAppCloneUserPair for user 0: false
03-06 23:31:01.717  1461  1733 V MediaProvider: Inserted id:1000000047 less than next row id backup:1000001000.
03-06 23:31:01.722  4515  4654 E BitmapFactory: Unable to decode stream: java.lang.NullPointerException
03-06 23:31:01.730  4515  4654 E AndroidRuntime: FATAL EXCEPTION: Thread-5
03-06 23:31:01.730  4515  4654 E AndroidRuntime: Process: app.clicksocial, PID: 4515
03-06 23:31:01.730  4515  4654 E AndroidRuntime: 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
03-06 23:31:01.730  4515  4654 E AndroidRuntime: 	at com.mr.flutter.plugin.filepicker.FileUtils.compressImage(FileUtils.java:106)
03-06 23:31:01.730  4515  4654 E AndroidRuntime: 	at com.mr.flutter.plugin.filepicker.FilePickerDelegate$2.run(FilePickerDelegate.java:104)
03-06 23:31:01.730  4515  4654 E AndroidRuntime: 	at java.lang.Thread.run(Thread.java:1012)

The code is question is the following (the crash happens on originalBitmap.compress because BitmapFactory.decodeFile returns null):

    public static Uri compressImage(Uri originalImageUri, int compressionQuality,Context context) {
        String originalImagePath = getRealPathFromURI(context,originalImageUri);
       Uri compressedUri=null;
       File compressedFile=null;
        try {
             compressedFile=createImageFile();
            Bitmap originalBitmap = BitmapFactory.decodeFile(originalImagePath);
            String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() +
                    "/FilePicker";
            // Compress and save the image
            FileOutputStream fos = new FileOutputStream(compressedFile);
            originalBitmap.compress(Bitmap.CompressFormat.JPEG, compressionQuality, fos);
            fos.flush();
            fos.close();
            compressedUri=Uri.fromFile(compressedFile);
        }catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return compressedUri;
    }

I tried adding some debug statements and re-building the library, but I can't seem to build it, because javax.naming is not included in Android:

Launching lib/main.dart on sdk gphone x86 64 in debug mode...
/home/luke/.pub-cache/hosted/pub.dev/file_picker-6.2.0/android/src/main/java/com/mr/flutter/plugin/filepicker/FileUtils.java:18: error: package javax.naming does not exist
import javax.naming.Context;
                   ^
/home/luke/.pub-cache/hosted/pub.dev/file_picker-6.2.0/android/src/main/java/com/mr/flutter/plugin/filepicker/FileUtils.java:64: error: cannot find symbol
    public static String getFileName(Uri uri, final Context context) {
                                                    ^
  symbol:   class Context
  location: class FileUtils

Platform

  • Android
  • iOS
  • Web
  • Desktop

Platform OS version

API 33

Flutter Version details

$ flutter doctor -v
[✓] Flutter (Channel beta, 3.20.0-1.2.pre, on Fedora Linux 39 (Workstation Edition) 6.7.7-200.fc39.x86_64, locale en_US.utf8)
    • Flutter version 3.20.0-1.2.pre on channel beta at /opt/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 1751123cde (2 weeks ago), 2024-02-21 22:06:22 -0800
    • Engine revision 299e852cb9
    • Dart version 3.4.0 (build 3.4.0-99.1.beta)
    • DevTools version 2.31.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /home/luke/Android/Sdk
    • Platform android-34, build-tools 33.0.1
    • Java binary at: /opt/android-studio/jbr/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • clang version 17.0.6 (Fedora 17.0.6-2.fc39)
    • cmake version 3.27.7
    • ninja version 1.11.1
    • pkg-config version 1.9.5

[✓] Android Studio (version 2022.2)
    • Android Studio at /opt/android-studio
    • Flutter plugin version 73.0.2
    • Dart plugin version 222.4582
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.87.0)
    • VS Code at /usr/share/code
    • Flutter extension version 3.85.20240301

[✓] VS Code (version 1.88.0-insider)
    • VS Code at /usr/share/code-insiders
    • Flutter extension version 3.79.20231130

[✓] Connected device (3 available)
    • sdk gphone x86 64 (mobile) • emulator-5554 • android-x64    • Android 13 (API 33) (emulator)
    • Linux (desktop)            • linux         • linux-x64      • Fedora Linux 39 (Workstation Edition) 6.7.7-200.fc39.x86_64
    • Chrome (web)               • chrome        • web-javascript • Google Chrome 122.0.6261.94

[✓] Network resources
    • All expected network resources are available.

• No issues found!

FYI this started happening in version 6.2.0. Version 6.1.1 does not have an issue.

I have pushed a PR to fix this, while it's being merged you can use my fork:

dependency_overrides:
  # Waiting for https://github.com/miguelpruivo/flutter_file_picker/issues/1455
  file_picker:
    git: https://github.com/easyhour/flutter_file_picker.git

Thank you @mauriziopinotti!