Sam
Sam

Reputation: 3064

Flutter pick image camera or gallery exception

I have a Flutter project in which I am trying to put MLKit for text OCR. I have crated a standalone Flutter project which works fine with MLKit. However when I put the same code and dependencies to my existing Flutter project it is not working as expected.

Below are the issues I am getting when trying to choosing the image...

  1. When I click on the gallery to choose the image it throws exception.

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

  2. When I am trying to choose camera it asks for the permission for the first time and then app gets closed.... 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"

  3. Once these exceptions thrown every subsequent tries it throws exception "PlatformException(already_active, image picker is already active, null)"

    cupertino_icons: ^0.1.2 firebase_auth: ^0.8.0+1 cloud_firestore: ^0.9.0 firebase_core: ^0.3.0 intl_translation: ^0.17.2 firebase_messaging: ^3.0.0 http: ^0.12.0+1 xml: ^3.3.1 firebase_storage: ^2.0.0 uuid: ^1.0.3 shared_preferences: ^0.5.0 flutter_staggered_grid_view: ^0.2.7 google_sign_in: ^4.0.0 flutter_signin_button: ^0.2.5 mlkit: ^0.9.0 path_provider: ^0.5.0+1 image_picker: ^0.5.0+3

I have followed below blog in order to implement MLKit https://medium.com/flutter-community/flutter-text-barcode-scanner-app-with-firebase-ml-kit-103db6b6dad7

Thank you

Upvotes: 0

Views: 8571

Answers (4)

Jabir Ishaq
Jabir Ishaq

Reputation: 177

First, the problem could be with the permissions, you need to add permission_handler for accessing the hardware like camera or storage etc.

Secondly try other application for camera other than the default app in our phone.

Upvotes: 0

Vicky Salunkhe
Vicky Salunkhe

Reputation: 11025

Try cleaning your app, use Flutter clean command.

Many times I have faced this sort of issues where there were no issue related to code and still something went wrong, but somehow Flutter clean tends to help resolve the issue.

Upvotes: 3

Tomáš Linhart
Tomáš Linhart

Reputation: 14329

You need to delete the build folder in your project. Once you do it and you start your project again, it will work.

Upvotes: 2

FlutterDevs
FlutterDevs

Reputation: 489

use image_picker plugin

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');
  }

Upvotes: 4

Related Questions