10
votes

In my flutter project I am getting exception whenever I am trying to pickup an image either from Camera or Gallery using the image_picker plugin of flutter.

For the very first time it asks for the permission and when I allow the camera it throws

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

After that, it throws an exception for every subsequent tries

PlatformException(already_active, Image picker is already active, null)

If I try to choose the camera or gallery even after restarting the app.

var imageSource;
if (source == CAMERA_SOURCE) {
    imageSource = ImageSource.camera;
} else {
    imageSource = ImageSource.gallery;
}

try {
     final file = await ImagePicker.pickImage(source: imageSource);
     if (file == null) {
     throw Exception('File is not available');
 }

Below are the dependencies:

cupertino_icons: ^0.1.2
firebase_auth: ^0.8.1
cloud_firestore: ^0.9.0+1
firebase_core: ^0.3.0+1
firebase_messaging: ^3.0.1
firebase_storage: ^2.0.1
intl_translation: ^0.17.3
http: ^0.12.0+1
xml: ^3.3.1
uuid: ^2.0.0
shared_preferences: ^0.5.1+1
flutter_staggered_grid_view: ^0.2.7
google_sign_in: ^4.0.1
flutter_signin_button: ^0.2.5
image_picker: ^0.5.0+2
mlkit: ^0.9.0
path_provider: ^0.5.0+1

Thanks for your time! I also tried to upgrade my flutter to the latest version.

7
Hey, Sam. Please edit your question and add some of your code so we can see what's going on. The functions that call the image picker, the button press etc. - George
I have added some more details - Sam
It's been awhile but if you are stil interested on this topic, it seems to be a bug: github.com/flutter/flutter/issues/28550 - kokeksibir
Thanks for the reply. After upgrading flutter and dependencies and flutter clean made it working... - Sam

7 Answers

5
votes

Finally I was able to resolve it.

I updated all my dependencies and flutter SDK and then I did Flutter clean and it started working..

Thanks all for your time and help

3
votes

Change in android/build.gradle classpath 'com.android.tools.build:gradle:3.5.4'

This worked for me.

2
votes

I updated package from 0.5.4+3 to ^0.6.5+2 and that solved my problem.

If you read the changelog, you will see every issue fixes: https://pub.dev/packages/image_picker#-changelog-tab-

0
votes

Removing and Reinstalling application as stated in the comments is fix the problem.

0
votes

In my case, I haven't given access to "Files". While connecting USB, give access as shown : enter image description here

0
votes

If you have channel based code i.e bridge between Native Android and Flutter.. In MainActivity on Activity Result .. try adding ..

super.onActivityResult(requestCode, resultCode, data)

0
votes

Anyway, I have the same issue when using Image Picker.

I have solved the trouble, it needs to provide permission to access the camera and storage memory on the release mode.

On Android: Add lines on file src/main/AndroidManifest.xml: inside the manifest tag

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

On IOS: Add lines on file Info.plist:

<key>NSPhotoLibraryUsageDescription</key>
<string>Allow access to photo library</string>

<key>NSCameraUsageDescription</key>
<string>Allow access to camera to capture photos</string>

Good luck, hope it will be useful!