11

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.

Sam
  • 2,972
  • 6
  • 34
  • 62
  • 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 Feb 17 '19 at 21:42
  • I have added some more details – Sam Feb 18 '19 at 15:07
  • 1
    It's been awhile but if you are stil interested on this topic, it seems to be a bug: https://github.com/flutter/flutter/issues/28550 – kokeksibir Mar 27 '19 at 21:22
  • Thanks for the reply. After upgrading flutter and dependencies and flutter clean made it working... – Sam Mar 28 '19 at 00:41

9 Answers9

5

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

Sam
  • 2,972
  • 6
  • 34
  • 62
3

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

This worked for me.

Yusuf
  • 771
  • 1
  • 6
  • 8
3

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)

ashutosh
  • 103
  • 9
  • Can you please elaborate or share a code snippet of usage. I am trying to pick image in flutter add-to-app approach. – bharats Mar 26 '22 at 12:28
3

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!

BOM Nguyen
  • 111
  • 1
  • 2
2

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-

Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

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

smrf
  • 349
  • 3
  • 16
  • i tried it, but is not helping. it is not a good use case for customer, to be forced to reinstall the app. :( – maerlyn Jan 22 '21 at 12:57
0

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

Dinesh Bala
  • 947
  • 7
  • 8
0

Please add this line in your MainActivity.kt class:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    super.onActivityResult(requestCode, resultCode, data); 
Hamed
  • 5,867
  • 4
  • 32
  • 56
rajdeep shah
  • 39
  • 1
  • 4
0

For the same error in my case, I added super onActivity result, maybe you are overriding any method on MainActivity with using super,

Ref answer here https://stackoverflow.com/a/65525354/15721679

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
    super.onActivityResult(requestCode, resultCode, data); // add this line.