安卓选择照片和拍照报错,iOS没有问题

来源:6-16 拍照APP开发-图片获取与图片展示【实战尝鲜】

慕粉13206522070

2020-02-03

安卓模拟机第一次打开相机没有问题,但是拍照之后没有显示到屏幕上,第二次再点击拍照就没有反应,控制台会报错,iOS没有问题,APP卸载重新安装、flutter clean都试过了,没有用,麻烦大家帮忙看下
代码如下:

import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

class PhotoAppPage extends StatefulWidget {
  @override
  _PhotoAppPageState createState() => _PhotoAppPageState();
}

class _PhotoAppPageState extends State<PhotoAppPage> {
  File _image;

  Future getImage(bool isTakePhoto) async {
    var image = await ImagePicker.pickImage(
        source: isTakePhoto ? ImageSource.camera : ImageSource.gallery);

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('拍照'),
      ),
      body: Center(
        child: _image == null ? Text('请拍照') : Image.file(_image),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _pickImage,
        tooltip: '拍照',
        child: Icon(Icons.add_a_photo),
      ),
    );
  }

  _pickImage() {
    showModalBottomSheet(
        context: context,
        builder: (context) => Container(
              height: 160,
              child: Column(
                children: <Widget>[
                  _item('拍照', true),
                  _item('从相册选择', false),
                ],
              ),
            ));
  }

  _item(String title, bool isTakePhoto) {
    return GestureDetector(
      child: ListTile(
        leading: Icon(isTakePhoto ? Icons.camera_alt : Icons.photo_library),
        title: Text(title),
        onTap: () => getImage(isTakePhoto),
      ),
    );
  }
}

报错如下:

E/flutter ( 4962): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(already_active, Image picker is already active, null)
E/flutter ( 4962): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter ( 4962): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter ( 4962): <asynchronous suspension>
E/flutter ( 4962): #2      ImagePicker.pickImage (package:image_picker/image_picker.dart:56:40)
E/flutter ( 4962): <asynchronous suspension>
E/flutter ( 4962): #3      _PhotoAppPageState.getImage (package:flutter_tt/photo_app_page.dart:15:35)
E/flutter ( 4962): <asynchronous suspension>
E/flutter ( 4962): #4      _PhotoAppPageState._item.<anonymous closure> (package:flutter_tt/photo_app_page.dart:59:22)
E/flutter ( 4962): #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:654:14)
E/flutter ( 4962): #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:729:32)
E/flutter ( 4962): #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter ( 4962): #8      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
E/flutter ( 4962): #9      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:312:7)
E/flutter ( 4962): #10     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter ( 4962): #11     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:222:20)
E/flutter ( 4962): #12     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter ( 4962): #13     GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter ( 4962): #14     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter ( 4962): #15     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter ( 4962): #16     _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter ( 4962): #17     _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter ( 4962): #18     _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter ( 4962): #19     _invoke1 (dart:ui/hooks.dart:263:10)
E/flutter ( 4962): #20     _dispatchPointerDataPacket (dart:ui/hooks.dart:172:5)
E/flutter ( 4962): 

应该是这个错误,但是不知道如何解决,麻烦帮忙看下

E/flutter ( 4962): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(already_active, Image picker is already active, null)

写回答

1回答

CrazyCodeBoy

2020-02-05

0
3
无名字的柜子
回复
慕粉13206522070
回复 慕粉13206522070我也整了半天,还好翻到你这,可以解决了!谢谢吖!
2020-02-08
共3条回复

Flutter从入门到进阶 实战携程网App 一网打尽核心技术

解锁Flutter开发新姿势,,系统掌握Flutter开发核心技术。

4788 学习 · 3270 问题

查看课程