问题
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.
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,
),
);
}
}
回答1:
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.
});
来源:https://stackoverflow.com/questions/64785733/cant-pick-image-from-gallery