Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Image picker is already active, null exception

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.

like image 262
Sam Avatar asked Feb 17 '19 20:02

Sam


5 Answers

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!

like image 153
BOM Nguyen Avatar answered Oct 31 '22 14:10

BOM Nguyen


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)

like image 36
ashutosh Avatar answered Oct 22 '22 06:10

ashutosh


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

like image 5
Sam Avatar answered Oct 31 '22 16:10

Sam


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

This worked for me.

like image 3
Yusuf Avatar answered Oct 31 '22 16:10

Yusuf


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-

like image 2
Blasanka Avatar answered Oct 31 '22 15:10

Blasanka