ImagePicker icon indicating copy to clipboard operation
ImagePicker copied to clipboard

NPE when using ImagePicker 1.2.2

Open songpr opened this issue 7 years ago • 14 comments

I have follow you guide and install ImagePicker on my project and compile successfully.

But it have NPE below.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:591) at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:565) at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:403) at com.mvc.imagepicker.ImagePicker.getPickImageIntent(ImagePicker.java:219) at com.mvc.imagepicker.ImagePicker.startChooser(ImagePicker.java:189) at com.mvc.imagepicker.ImagePicker.pickImage(ImagePicker.java:180) at com.mvc.imagepicker.ImagePicker.pickImage(ImagePicker.java:79) at com.todayfortomorrow.dumex.childinfo.ChildInfoActivity$5.onClick(ChildInfoActivity.java:364) at android.view.View.performClick(View.java:5181) at android.view.View$PerformClick.run(View.java:20887) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5942) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)

when run on following code

Intent chooseImageIntent = ImagePicker.getPickImageIntent(ChildInfoActivity.this, "ChildInfo");

Do you have any advice how to solve this?

songpr avatar Feb 21 '18 09:02 songpr

After debug the app failed at

takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", ImageUtils.getTemporalFile(context, String.valueOf(mPickImageRequestCode))));

in this code

if (!mGalleryOnly) {
        // Camera action will fail if the app does not have permission, check before adding intent.
        // We only need to add the camera intent if the app does not use the CAMERA permission
        // in the androidmanifest.xml
        // Or if the user has granted access to the camera.
        // See https://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE
        if (!appManifestContainsPermission(context, Manifest.permission.CAMERA) || hasCameraAccess(context)) {
            Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            takePhotoIntent.putExtra("return-data", true);
            takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider",
                            ImageUtils.getTemporalFile(context, String.valueOf(mPickImageRequestCode))));
            //Uri.fromFile(ImageUtils.getTemporalFile(context, String.valueOf(mPickImageRequestCode))));
            intentList = addIntentsToList(context, intentList, takePhotoIntent);
        }
    }

songpr avatar Feb 21 '18 09:02 songpr

@songpr

You need to add Runtime Uri Grant Permissions.

` if (!mGalleryOnly) { // Camera action will fail if the app does not have permission, check before adding intent. // We only need to add the camera intent if the app does not use the CAMERA permission // in the androidmanifest.xml // Or if the user has granted access to the camera. // See https://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE if (!appManifestContainsPermission(context, Manifest.permission.CAMERA) || hasCameraAccess(context)) { Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); takePhotoIntent.putExtra("return-data", true);

            Uri imageUri =  FileProvider.getUriForFile(context,BuildConfig.APPLICATION_ID + ".provider",ImageUtils.getTemporalFile(context, String.valueOf(mPickImageRequestCode)));
            takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);

            //Grant Runtime URi permissions
            List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(takePhotoIntent, PackageManager.MATCH_DEFAULT_ONLY);
            for (ResolveInfo resolveInfo : resInfoList)
            {
                String packageName = resolveInfo.activityInfo.packageName;
                context.grantUriPermission(packageName, imageUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }


            intentList = addIntentsToList(context, intentList, takePhotoIntent);
        }
    }`

Code-N-K avatar Feb 21 '18 09:02 Code-N-K

@Code-N-K Thanks, it's work in my local test,

songpr avatar Feb 22 '18 03:02 songpr

This issue duplicate with this issue NullPointerException on OnePlus3 Oreo

songpr avatar Feb 22 '18 08:02 songpr

I've forked and apply change as shown in https://github.com/songpr/ImagePicker/blob/develop/library/src/main/java/com/mvc/imagepicker/ImagePicker.java

However, the same error is happen.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:591) at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:565) at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:403) at com.mvc.imagepicker.ImagePicker.getPickImageIntent(ImagePicker.java:219) at com.mvc.imagepicker.ImagePicker.startChooser(ImagePicker.java:189) at com.mvc.imagepicker.ImagePicker.pickImage(ImagePicker.java:180)

The change is work only when I took source code and build in the application, But do not work when add to project as library below.

dependencies { compile 'com.github.songpr:ImagePicker:f96bf8d' ....

songpr avatar Feb 22 '18 10:02 songpr

So, we shall copy the library code, into our apps, in order, to get rid of the exception? Or Author will release newer version, with handling that exception?

PokerAsh avatar Feb 23 '18 10:02 PokerAsh

I have updated the author..

As we have the fix we could do it with libarary code also..

On Fri 23. Feb 2018 at 4:07 PM, Yernar Mendigarin [email protected] wrote:

So, we shall copy the library code, into our apps, in order, to get rid of the exception? Or Author will release newer version, with handling that exception?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Mariovc/ImagePicker/issues/45#issuecomment-367973271, or mute the thread https://github.com/notifications/unsubscribe-auth/AULMjWbb0NvulLNvrKEkRFklbIMIG0Uxks5tXpTjgaJpZM4SNRnT .

Code-N-K avatar Feb 24 '18 08:02 Code-N-K

Until we get a new version with the said changes using the libarary will not work.

On Thu 22. Feb 2018 at 4:18 PM, songpr [email protected] wrote:

I've forked and apply change as shown in https://github.com/songpr/ImagePicker/blob/develop/library/src/main/java/com/mvc/imagepicker/ImagePicker.java

However, the same error is happen.

java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:591) at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:565) at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:403) at com.mvc.imagepicker.ImagePicker.getPickImageIntent(ImagePicker.java:219) at com.mvc.imagepicker.ImagePicker.startChooser(ImagePicker.java:189) at com.mvc.imagepicker.ImagePicker.pickImage(ImagePicker.java:180)

The change is work only when I took source code and build in the application, But do not work when add to project as library.

dependencies { compile 'com.github.songpr:ImagePicker:f96bf8d'

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Mariovc/ImagePicker/issues/45#issuecomment-367643296, or mute the thread https://github.com/notifications/unsubscribe-auth/AULMjevTWmmma2ZkFYmdoeP6FoX_glfXks5tXUYUgaJpZM4SNRnT .

Code-N-K avatar Feb 24 '18 08:02 Code-N-K

I've got another question. for some reason, it doens't seems to have a runtime permissions right? We have to handle the camera use and read from internal storage permissions from ourselves? Or I am missing something? The thing is that, whenever I am trying to turn on/off the permissions from my testing phone(also, re-installing the app) it never gives me the dialog about using it...

PokerAsh avatar Feb 24 '18 12:02 PokerAsh

Manage by Yourself.

On Sat, Feb 24, 2018 at 6:11 PM, Yernar Mendigarin <[email protected]

wrote:

I've got another question. for some reason, it doens't seems to have a runtime permissions right? We have to handle the camera use and read from internal storage permissions from ourselves? Or I am missing something? The thing is that, whenever I am trying to turn on/off the permissions from my testing phone(also, re-installing the app) it never gives me the dialog about using it...

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Mariovc/ImagePicker/issues/45#issuecomment-368225913, or mute the thread https://github.com/notifications/unsubscribe-auth/AULMjfk21DuZsn35EPIaCjwKGlYy1xFJks5tYAOSgaJpZM4SNRnT .

Code-N-K avatar Mar 01 '18 10:03 Code-N-K

It seems like the only workaround for now is copying the source code instead of using it as a dependency. What worked for me:

  1. Copy all the files in com/mvc/imagepicker to my project's src/main/java folder.
  2. Adding a "pick_image_intent_text" in strings.xml
  3. Modifying the imports for R and BuildConfig in ImagePicker.java

andrew-lim avatar Mar 29 '18 11:03 andrew-lim

You can fork and fix in your repository then add dependency to your forked project. Try this https://github.com/Mariovc/ImagePicker/pull/46/commits/ae5687b5ea071e6015e1b46ec1f360dee285beae

songpr avatar Apr 05 '18 05:04 songpr

Add this to the application tag of your Android Manifest:

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.mvc.imagepicker.provider"
            android:exported="false"
            android:grantUriPermissions="true"
            tools:replace="android:authorities">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"
                tools:replace="android:resource"/>
        </provider>

euri16 avatar May 10 '18 22:05 euri16

@euri it worked.But when i im opening camera app from picker it gives this error java.io.FileNotFoundException: /cache/i_prefix_234 (Permission denied)

tell me what should i do???

git1998 avatar Jun 20 '18 12:06 git1998