相机无法打开应用程序进行拍照并上传

时间:2019-01-29 10:26:24

标签: dart flutter

当我单击头像并要上传照片时,我需要选择从图库上传照片或拍照。如果单击以从画廊中选择,一切正常。但是,如果单击以拍照,对话框将关闭,并且什么也没有发生。

  

帮助我无法摆脱这个问题!

My GitHub with project

    import 'dart:io';
import 'package:logining/account_screen/button_countries.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

String imagePath = 'images/user.png';

class AccountScreen extends StatefulWidget {
  _AccountScreenState createState() => _AccountScreenState();
}

class _AccountScreenState extends State<AccountScreen> {
  imageSelectorGallery() async {
    final File imageFile = await ImagePicker.pickImage(
      source: ImageSource.gallery,
    );
    setState(() {
      if (imageFile != null) imagePath = imageFile.path;
    });
  }

  imageSelectorCamera() async {
    final File imageFile = await ImagePicker.pickImage(
      source: ImageSource.camera,
    );
    setState(() {
      if (imageFile != null) imagePath = imageFile.path;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('AvatarScreen'),
        ),
        resizeToAvoidBottomPadding: false,
        body: ListView(children: <Widget>[
          Container(
            child: Form(
              child: Column(children: <Widget>[
                SizedBox(
                  height: 30,
                ),
                IconButton(
                    iconSize: 150,
                    icon: CircleAvatar(
                      backgroundImage: AssetImage(imagePath),
                      radius: 150,
                    ),
                    onPressed: () {
                      showDialog(
                          context: context,
                          builder: (BuildContext context) => AlertDialog(
                                title: Text('Choise a method to save a image'),
                                actions: <Widget>[
                                  FlatButton(
                                    child: Text('Make a photo'),
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                      imageSelectorCamera();
                                    },
                                  ),
                                  FlatButton(
                                    child: Text('Photo from gallery'),
                                    onPressed: () {
                                      Navigator.of(context).pop();
                                      imageSelectorGallery();
                                    },
                                  ),
                                ],
                              ));
                    }),
              ]),
            ),
          ),

文件pubspeck.yaml

 dependencies:
  http: ^0.12.0
  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.
  cupertino_icons: ^0.1.2
  image_picker: 
  url_launcher: ^4.0.3

dev_dependencies:
  flutter_test:
    sdk: flutter

1 个答案:

答案 0 :(得分:0)

AndroidManifest.xml文件提供权限。

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />