简体   繁体   English

无法从图库中选择图像

[英]Can't pick image from gallery

I am coding an application for recognizing plant species.我正在编写用于识别植物物种的应用程序。 I want to pick an image and show that on screen but i can't.我想选择一个图像并将其显示在屏幕上,但我不能。 Image returns null.图像返回空值。 Although i choose photo from gallery, it still says "no image selected".虽然我从图库中选择照片,但它仍然显示“未选择图像”。

I also added read_external_storage permission to android manifests.我还向 android manifests 添加了 read_external_storage 权限。

如你看到的。

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

class PlantSpeciesRecognition extends StatefulWidget {
  var model;

  PlantSpeciesRecognition(this.model);

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

class _PlantSpeciesRecognitionState extends State<PlantSpeciesRecognition> {
  File _image;
  bool _busy = false;
  List _recognitions;


  Future chooseImageGallery() async {
    debugPrint("choose image function");
    var image = await ImagePicker().getImage(source: ImageSource.gallery);
    //var image = await ImagePicker.pickImage(source: ImageSource.gallery);

    if (image == null) {
      debugPrint("choose image if");
      return;
    }
    //await analyzeTFLite();

    setState(() {
      debugPrint("choose image set state");
      _busy = true;
      _image = image as File;
    });
  }
   
  @override
  Widget build(BuildContext context) {
    List<Widget> stackChildren = [];
    Size size = MediaQuery.of(context).size;

    //stackChildren.clear();

    stackChildren.add(Positioned(
      top: 0.0,
      left: 0.0,
      width: size.width,
      child: _image == null ? Text("No Image Selected") : Image.file(_image),
    ));

    return Scaffold(
      appBar: AppBar(
        title: const Text('Plant Species Recognition'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: chooseImageGallery,
        tooltip: 'Pick Image',
        child: Icon(Icons.image),
      ),
      body: Stack(
        children: stackChildren,
      ),
    );
  }
}

this is normal because it is not the file that is returned.这是正常的,因为它不是返回的文件。 Correct the following line:更正以下行:

setState(() {
      debugPrint("choose image set state");
      _busy = true;
      _image = File(image.path); //change this line.
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM