React-native-image-picker: Android app crashes on launching camera to take photo

Created on 3 Jan 2018  路  31Comments  路  Source: react-native-image-picker/react-native-image-picker

Description

RN 0.51.0
Android 5.1
react-native-image-picker 0.26.7
Android app crashes on launching camera to take photo

How to repeat issue and example

How to solve?

Solution

Additional Information

stale

Most helpful comment

I sloved this problem (for my case) by copying this:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider>

from the package into my main project android manifest.

All 31 comments

Please paste the stacktrace.

@gengjiawen
/com.rndivi E/AndroidRuntime: FATAL EXCEPTION: mqt_native_modules Process: com.rndivi, PID: 19045 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:560) at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534) at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376) at com.imagepicker.utils.RealPathUtil.compatUriFromFile(RealPathUtil.java:31) at com.imagepicker.ImagePickerModule.launchCamera(ImagePickerModule.java:257) at com.imagepicker.ImagePickerModule.launchCamera(ImagePickerModule.java:206) at com.imagepicker.ImagePickerModule$2.onTakePhoto(ImagePickerModule.java:162) at com.imagepicker.utils.UI$1.onClick(UI.java:54) at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:959) at android.widget.AdapterView.performItemClick(AdapterView.java:310) at android.widget.AbsListView.performItemClick(AbsListView.java:1263) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3380) at android.widget.AbsListView.onTouchUp(AbsListView.java:4354) at android.widget.AbsListView.onTouchEvent(AbsListView.java:4059) at android.widget.ListView.onTouchEvent(ListView.java:4287) at android.view.View.dispatchTouchEvent(View.java:8662) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2513) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2157) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2519) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2171) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2529) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1802) at android.app.Dialog.dispatchTouchEvent(Dialog.java:765) at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2486) at android.view.View.dispatchPointerEvent(View.java:8868) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4771) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4603) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4079) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4132) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4098) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4235) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4106) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4292) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4079) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4132) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4098) at android.view.ViewRootImpl$InputStage.ap

Have you tried this, looks like the same question.

npm install gengjiawen/react-native-image-picker#bugfix/android_photo_crash -S

@gengjiawen I tried three times锛宐ut got the same error. Android 5.1

can you post the package.json ?

{
"name": "RNDIVI",
"version": "0.0.1",
"private": true,
"scripts": {
"configureJPush": "node node_modules/jpush-react-native/JPushConfiguration.js",
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"jcore-react-native": "^1.2.2",
"jpush-react-native": "^2.1.6",
"react": "16.0.0",
"react-native": "0.51.0",
"react-native-baidu-map": "^0.6.0",
"react-native-blur": "^3.2.2",
"react-native-camera": "^0.6.0",
"react-native-image-picker": "^0.26.7",
"react-native-navigation": "^1.1.315",
"react-native-parabolic": "^1.1.1",
"react-native-percentage-circle": "^1.0.6",
"react-native-scrollable-tab-view": "^0.8.0",
"react-native-splash-screen": "^3.0.6",
"react-native-swiper": "^1.5.13",
"react-native-tab-navigator": "^0.3.4",
"react-native-vector-icons": "^4.4.3",
"react-native-wechat": "^1.9.9",
"react-navigation": "^1.0.0-beta.22"
},
"devDependencies": {
"babel-jest": "22.0.3",
"babel-preset-react-native": "4.0.0",
"jest": "22.0.3",
"react-test-renderer": "16.0.0"
},
"jest": {
"preset": "react-native"
}
}

you are still using the official repo, not my patch.

It should be look like this:

"react-native-image-picker": "github:gengjiawen/react-native-image-picker#bugfix/android_photo_crash"

got the same error, App is crashes
image
01-04 10:39:51.072 8170-8429/com.rndivi E/AndroidRuntime: FATAL EXCEPTION: mqt_native_modules
Process: com.rndivi, PID: 8170
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:560)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:534)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:376)
at com.imagepicker.utils.RealPathUtil.compatUriFromFile(RealPathUtil.java:31)
at com.imagepicker.ImagePickerModule.launchCamera(ImagePickerModule.java:257)
at com.imagepicker.ImagePickerModule.launchCamera(ImagePickerModule.java:206)
at com.imagepicker.ImagePickerModule$2.onTakePhoto(ImagePickerModule.java:162)
at com.imagepicker.utils.UI$1.onClick(UI.java:54)
at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:959)

Tried delete node_modules and reinstall ?

yes, i delete the official repo, and npm install gengjiawen/react-native-image-picker#bugfix/android_photo_crash -S.
Got the same error.

You need reinstall the app too, Did you do that ?

yes. App was reinstall.

Weird, can you see in node_modules did my patch actually there, if they do in that place, then there is little thing I can do.

AndroidManifest.xml

xmlns:android="http://schemas.android.com/apk/res/android"
package="com.imagepicker"
>

android:name=".utils.ImagePickerProvider"
android:authorities="${applicationId}.image.provider"
android:exported="false"
android:grantUriPermissions="true">
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_image_picker_paths" />



ImagePickerProvider.java
package com.imagepicker.utils;

import android.support.v4.content.FileProvider;

public class ImagePickerProvider extends FileProvider {
}
RealPathUtil.java
final String packageName = context.getApplicationContext().getPackageName();
final String authority = new StringBuilder(packageName).append(".image.provider").toString();

@gengjiawen

I tried your patch, but still getting same issue. Any idea?

Android Jellybean

01-07 12:15:09.330 4717-4770/? E/AndroidRuntime: FATAL EXCEPTION: mqt_native_modules
                                                 java.lang.NullPointerException
                                                     at android.app.ActivityManagerProxy.grantUriPermission(ActivityManagerNative.java:2918)
                                                     at android.app.ContextImpl.grantUriPermission(ContextImpl.java:1350)
                                                     at android.content.ContextWrapper.grantUriPermission(ContextWrapper.java:453)
                                                     at android.content.ContextWrapper.grantUriPermission(ContextWrapper.java:453)
                                                     at com.imagepicker.ImagePickerModule.launchCamera(ImagePickerModule.java:285)
                                                     at com.imagepicker.ImagePickerModule.launchCamera(ImagePickerModule.java:206)
                                                     at com.imagepicker.ImagePickerModule$2.onTakePhoto(ImagePickerModule.java:162)
                                                     at com.imagepicker.utils.UI$1.onClick(UI.java:54)
                                                     at android.support.v7.app.AlertController$AlertParams$3.onItemClick(AlertController.java:959)
                                                     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
                                                     at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
                                                     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2859)
                                                     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3545)
                                                     at android.view.View.dispatchTouchEvent(View.java:7127)
                                                     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
                                                     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
                                                     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
                                                     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
                                                     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
                                                     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
                                                     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
                                                     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
                                                     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
                                                     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
                                                     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
                                                     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
                                                     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
                                                     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
                                                     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
                                                     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
                                                     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
                                                     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
                                                     at android.app.Dialog.dispatchTouchEvent(Dialog.java:736)
                                                     at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
                                                     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
                                                     at android.view.View.dispatchPointerEvent(View.java:7307)
                                                     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
                                                     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
                                                     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
                                                     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
                                                     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
                                                     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
                                                     at android.os.MessageQueue.nativePollOnce(Native Method)
                                                     at android.os.MessageQueue.next(MessageQueue.java:125)
                                                     at android.os.Looper.loop(Looper.java:124)
                                                     at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
                                                     at java.lang.Thread.run(Thread.java:856)

@DIVIBEAR were u able to fix your issue?

@gengjiawen for some reason if we comment this code

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
      List<ResolveInfo> resInfoList = reactContext.getPackageManager().queryIntentActivities(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY);
      for (ResolveInfo resolveInfo : resInfoList) {
        String packageName = resolveInfo.activityInfo.packageName;
        System.out.println("ggg - "+packageName+"-"+cameraCaptureURI+"-"+Intent.FLAG_GRANT_WRITE_URI_PERMISSION+"-"+Intent.FLAG_GRANT_READ_URI_PERMISSION);
        reactContext.grantUriPermission(packageName, cameraCaptureURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
      }
    }

It works..

Can you provide an repro repo so I can take a look.

Do we have any update on this? I have the exact same issue using android 6.0.1 API 23, it crashes when I try to open the camera to take a picture, but when I try with video it works perfectly...

I faced the same problem. Seems the problem in 'noData' option on android. After I removed the option 'noData', I was able to take photo.

Also having this issue, tried with the bugfix branch but still crashing when selecting camera. Library works fine.

I sloved this problem (for my case) by copying this:
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider>

from the package into my main project android manifest.

I tried everything but the only thing that worked for me is @mistenkt's solution, on top of the regular branch of this project.

@mistenkt try to create file react-native-image-picker/src/main/res/values/public.xml and put the next content into it

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <public />
</resources>

Build.VERSION.SDK_INT < 24

public static @Nullable Uri compatUriFromFile(@NonNull final Context context, @NonNull final File file){
        Uri result = null;
        if (Build.VERSION.SDK_INT < 24) {
            result = Uri.fromFile(file);
        }
        else {
            final String packageName = context.getApplicationContext().getPackageName();
            final String authority =  new StringBuilder(packageName).append(".provider").toString();
            try {
                result = FileProvider.getUriForFile(context, authority, file);
            }
            catch(IllegalArgumentException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

Is there any solution to this? This is happening to me sometimes in the emulator, I haven't tried yet in a device..

If you guys don't want this error you can just get back to older version
compile 'com.github.Mariovc:ImagePicker:1.2.0'

I found a very efficient way to deal with this bug it seems :
Use the options maxWidth, maxHeight and/or quality to reduce the size of the pictures taken (depending on your use case, having pictures 3200x3200 might be useless). I found that for my need a 1200x1200 max + 0.9 quality works almost 100% of the time ! Hope this helps

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and I will leave this open.

Closing this issue after a prolonged period of inactivity. Fell free to reopen this issue, if this still affecting you.

Was this page helpful?
0 / 5 - 0 ratings