3

I am trying to give users a method to add a photo from their gallery or their camera but, flutter keeps crashing after the button is pressed.

I've implemented the image picker dependency as well as Dart:io, migrated to Android X but, still no luck. Here is some of my code:

class _PreferencesState extends State<_Preferences>
    with TickerProviderStateMixin {


File _image;
getImage(bool isCamera) async {
   File image;

if (isCamera) {
  image = await ImagePicker.pickImage(source: ImageSource.camera);
} else {
  image = await ImagePicker.pickImage(source: ImageSource.gallery);
}

setState(() {
  _image = image;
  });
}

Then called here:

  FlatButton(
      textColor: Theme.of(context).primaryColor,
      child: Text('Use Camera'),
      onPressed: () {
          getImage(true);
      },
  ),
  _image == null
      ? Container()
      : Image.file(
          _image,
          height: 300,
          width: 300,
      ),    

Every time I call the method I get this error:

Exception has occurred. PlatformException (PlatformException(error, 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, null))

I thought I changed the image type from null so it wouldn't be called but, I'm not sure what else to do here.

2
  • 1
    Can you share your log, and try cleaning your project once, use flutter clean Commented Apr 26, 2019 at 20:20
  • is this code asking you about where you want to pick the picture from gallery or camera??? Commented Oct 14, 2019 at 9:00

2 Answers 2

5

The crash could be due to camera permission. Image picker plugin has removed the camera permission condition. When we access camera, app asks us for permission, but this condition in not supported anymore. We need to ask the permission manually using permission_handler plugin. Read more details about this change here and see this link on how to add runtime permission using permission_handler. Hope this resolves your issue.

Sign up to request clarification or add additional context in comments.

Comments

0

This might not be the best way to do it but if you want to solve the solution more fast and easy you can just clean all your packages and delete the project then build it again. Use the commands below for the whole process:

Navigate to the folder with the pubspec.yaml file and perform the functions below on your terminal:

[user@machine~]$ flutter clean
[user@machine~]$ adb uninstall com.example.myfirst
[user@machine~]$ flutter build apk
[user@machine~]$ flutter pub get
[user@machine~]$ flutter doctor -v
[user@machine~]$ flutter pub upgrade --major-versions
[user@machine~]$ flutter run

When the the android sdk phone starts ensure you allow the app to access your phone files when you click the button to upload images/files from the gallery

N/B: This worked for my app may it might work on yours or not.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.