3

我正在尝试使用图库或相机选择图像并使用 image_picker 显示。

当我在 android 中运行应用程序时,我可以选择图像但不显示。相比之下,我第一次在控制台中获得关注。

I/HwViewRootImpl(11213): removeInvalidNode jank list 中的所有节点都超时

如果我重复相同的操作,它会在每次按下按钮而不是打开画廊或相机时提供以下信息。

I/flutter (11213): PlatformException(already_active, Image picker is already active, null)

我从搜索中找到了以下解决方案,但没有解决我的问题。

以下是我用于检索图像的代码:

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

class CameraApp extends StatefulWidget {
  @override
  _CameraAppState createState() => _CameraAppState();
}

class _CameraAppState extends State<CameraApp> {
  File imageFile;

  @override
  void initState() {

    super.initState();
  }

  Future _getImage(int type) async {
    print("Called Image Picker");
    var image = await ImagePicker.pickImage(
      source: type == 1 ? ImageSource.camera : ImageSource.gallery,
    );

    setState(() {
      print("$image.path");
      imageFile = image;
    });
  }

  Future<void> retrieveLostData() async {
    final LostDataResponse response = await ImagePicker.retrieveLostData();
    if (response == null) {
      return;
    }
    if (response.file != null) {
      setState(() {
        if (response.type == RetrieveType.image) {
          imageFile = response.file;
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Image Editor"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            imageFile != null
                ? Image.file(
                    imageFile,
                    height: MediaQuery.of(context).size.height / 2,
                  )
                : Text("Image editor"),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: new Text("Add Slip"),
                content: Row(
                  children: <Widget>[
                    Expanded(
                      child: new FlatButton(
                        child: new Text("Camera"),
                        onPressed: () {
                          _getImage(1);
                          Navigator.pop(context);
                        },
                      ),
                    ),
                    Expanded(
                      child: new FlatButton(
                        child: new Text("Gallery"),
                        onPressed: () {
                          _getImage(2);
                          Navigator.pop(context);
                        },
                      ),
                    )
                  ],
                ),
              );
            },
          );
        },
        tooltip: 'Pick Image',
        child: Icon(Icons.camera),
      ),
    );
  }
}
4

1 回答 1

0

我已经尝试了您共享的示例代码,但由于某种原因遇到了编译器问题,但与您的问题不同。因此,我尝试调试您的代码。这是固定代码:

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  File imageFile;

  @override
  void initState() {
    super.initState();
  }

  Future _getImage(int type) async {
    print("Called Image Picker");
    var image = await await ImagePicker.platform.pickImage(
      source: type == 1 ? ImageSource.camera : ImageSource.gallery,
    );

    setState(() {
      print("$image.path");
      imageFile = File(image.path);
    });
  }

  Future<void> retrieveLostData() async {
    final LostData response = await ImagePicker.platform.retrieveLostData();
    if (response == null) {
      return;
    }
    if (response.file != null) {
      setState(() {
        if (response.type == RetrieveType.image) {
          imageFile = response.file as File;
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Image Editor"),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            imageFile != null
                ? Image.file(
                    imageFile,
                    height: MediaQuery.of(context).size.height / 2,
                  )
                : Text("Image editor"),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: new Text("Add Slip"),
                content: Row(
                  children: <Widget>[
                    Expanded(
                      child: new FlatButton(
                        child: new Text("Camera"),
                        onPressed: () {
                          _getImage(1);
                          Navigator.pop(context);
                        },
                      ),
                    ),
                    Expanded(
                      child: new FlatButton(
                        child: new Text("Gallery"),
                        onPressed: () {
                          _getImage(2);
                          Navigator.pop(context);
                        },
                      ),
                    )
                  ],
                ),
              );
            },
          );
        },
        tooltip: 'Pick Image',
        child: Icon(Icons.camera),
      ),
    );
  }
}

很少有修复是这些行:

var image = await await ImagePicker.pickImage(
      source: type == 1 ? ImageSource.camera : ImageSource.gallery,
    );

它在编译器中有错误,所以我将其更改为:

var image = await await ImagePicker.platform.pickImage(
      source: type == 1 ? ImageSource.camera : ImageSource.gallery,
    );

与这些行相同:

Future<void> retrieveLostData() async {
    final LostData response = await ImagePicker.retrieveLostData();
    if (response == null) {
      return;
    }
    if (response.file != null) {
      setState(() {
        if (response.type == RetrieveType.image) {
          imageFile = response;
        }
      });
    }
  }

固定版本:

 Future<void> retrieveLostData() async {
    final LostData response = await ImagePicker.platform.retrieveLostData();
    if (response == null) {
      return;
    }
    if (response.file != null) {
      setState(() {
        if (response.type == RetrieveType.image) {
          imageFile = response.file as File;
        }
      });
    }
  }

和这个

setState(() {
      print("$image.path");
      imageFile = image;
    }

对此:

 setState(() {
          print("$image.path");
          imageFile = File(image.path);
        }

原因可能是image_picker我正在使用的版本。目前我正在使用image_picker: ^0.7.4.

这是实际输出:

在此处输入图像描述

如果您在 Android API 版本 30 中运行它,我也会遇到一个问题,您会收到以下错误:

Unhandled Exception: PlatformException(no_available_camera, No cameras available for taking pictures., null, null)

解决方法是添加此 GitHub 帖子<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>中提到的清单。

于 2021-04-15T21:49:16.867 回答