react-native-image-picker: testOnImmutable fails with java.lang.NullPointerException

Description

testOnImmutable() in ImageConfigTest.java throws NPE at line 29: ImageConfig updated = original.withOriginalFile(null);.

withOriginalFile has a @Nullable annotation for the parameter - but I think the subsequent line String extension = MimeTypeMap.getFileExtensionFromUrl(original.getAbsolutePath()); causes NPE.

This test failure causes gradlew build or gradlew assembleRelease to fail on an app that uses this package.

How to repeat issue and example

  • Install react-native-image-picker per README instructions.
  • Import to app using import ImagePicker from 'react-native-image-picker';. Use in app (my example is here)
  • Run ./gradlew build or ./gradlew assembleRelease from the android folder of the app

Will get the following error during build:

> Task :react-native-image-picker:testDebugUnitTest 

com.imagepicker.testing.media.ImageConfigTest > testOnImmutable FAILED
    java.lang.NullPointerException at ImageConfigTest.java:29

5 tests completed, 1 failed


FAILURE: Build failed with an exception.

Solution

Opened PR #973 . I worked around using the following change in ImageConfig.java - please let me know if there’s a better way to handle this:

    public @NonNull ImageConfig withOriginalFile(@Nullable final File original)
    {
        if (original != null) {
        //if it is a GIF file, always set quality to 100 to prevent compression
        String extension = MimeTypeMap.getFileExtensionFromUrl(original.getAbsolutePath());
        int quality = this.quality;
        if(extension.contains("gif")){
            quality = 100;
        }
    }

        return new ImageConfig(
                original, this.resized, this.maxWidth,
                this.maxHeight, quality, this.rotation,
                this.saveToCameraRoll
        );
    }

Additional Information

  • React Native version: 0.57.4
  • Platform: Android
  • Development Operating System: Ubuntu 18.04
  • Dev tools: react-native-cli

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (1 by maintainers)

Commits related to this issue

Most upvoted comments

@spsaucier-bakkt I found that, with a device attached, setup for USB debugging, and listed in add devices simply running react-native run-android works (no --deviceId flag). There is something about the --deviceId flag that causes problems.

While this certainly isn’t a fix, I hope this helps you all test on devices!

This issue still happens when attempting to build a APK. Not sure why it was closed. This library prevents my application from building:

> Task :react-native-image-picker:testDebugUnitTest

com.imagepicker.testing.ImagePickerModuleTest > testCancelTakingPhoto FAILED
    org.apache.tools.ant.BuildException
        Caused by: org.apache.maven.artifact.resolver.ArtifactResolutionException
            Caused by: org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException
                Caused by: org.apache.maven.project.ProjectBuildingException
                    Caused by: org.apache.maven.project.ProjectBuildingException
                        Caused by: org.apache.maven.artifact.resolver.ArtifactNotFoundException
                            Caused by: org.apache.maven.wagon.ResourceDoesNotExistException

com.imagepicker.testing.media.ImageConfigTest > testOnImmutable FAILED
    org.apache.tools.ant.BuildException
        Caused by: org.apache.maven.artifact.resolver.ArtifactResolutionException
            Caused by: org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException
                Caused by: org.apache.maven.project.ProjectBuildingException
                    Caused by: org.apache.maven.project.ProjectBuildingException
                        Caused by: org.apache.maven.artifact.resolver.ArtifactNotFoundException
                            Caused by: org.apache.maven.wagon.ResourceDoesNotExistException

com.imagepicker.testing.media.ImageConfigTest > testParsingOptions FAILED
    org.apache.tools.ant.BuildException
        Caused by: org.apache.maven.artifact.resolver.ArtifactResolutionException
            Caused by: org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException
                Caused by: org.apache.maven.project.ProjectBuildingException
                    Caused by: org.apache.maven.project.ProjectBuildingException
                        Caused by: org.apache.maven.artifact.resolver.ArtifactNotFoundException
                            Caused by: org.apache.maven.wagon.ResourceDoesNotExistException

com.imagepicker.testing.media.ImageConfigTest > testUseOriginal FAILED
    org.apache.tools.ant.BuildException
        Caused by: org.apache.maven.artifact.resolver.ArtifactResolutionException
            Caused by: org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException
                Caused by: org.apache.maven.project.ProjectBuildingException
                    Caused by: org.apache.maven.project.ProjectBuildingException
                        Caused by: org.apache.maven.artifact.resolver.ArtifactNotFoundException
                            Caused by: org.apache.maven.wagon.ResourceDoesNotExistException

com.imagepicker.testing.media.ImageConfigTest > testGetActualFile FAILED
    org.apache.tools.ant.BuildException
        Caused by: org.apache.maven.artifact.resolver.ArtifactResolutionException
            Caused by: org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException
                Caused by: org.apache.maven.project.ProjectBuildingException
                    Caused by: org.apache.maven.project.ProjectBuildingException
                        Caused by: org.apache.maven.artifact.resolver.ArtifactNotFoundException
                            Caused by: org.apache.maven.wagon.ResourceDoesNotExistException

5 tests completed, 5 failed

> Task :react-native-image-picker:testDebugUnitTest FAILED

FAILURE: Build failed with an exception.

Follow up

I got this working. The steps I took were (all from my project root):

  1. rm -rf android/build
  2. rm -rf android/app/build (we have some custom Android modules)
  3. rm -rf ./node_modules/
  4. npm it (to install all project dependencies and run our tests)
  5. npx react-native start (we use npx to avoid having to install global node modules)
  6. npx react-native run-android --deviceId $DEVICE_ID

I’m guessing that at some point something got out of sync. Blowing everything away and starting from scratch seems to have fixed the issue! 🤘

I randomly found this thread on flutter which apparently had the same issue: https://github.com/flutter/flutter/issues/48943#issuecomment-575014355

Jan 16: Maven has changed their access policies as of today: https://support.sonatype.com/hc/en-us/articles/360041287334

Flutter fixed it here: https://github.com/flutter/plugins/pull/2475.

Looks like we need to bump roboelectric? cc @janicduplessis since you originally fixed the issue.

I have the same issue on version 0.27.2

But on 0.26.10 everything works fine