我是 flutter 的初学者。我正在检查在flutter中使用image_picker。但是,当我尝试使用Andorid手机(10)检查image_picker时,它不会显示。我不知道发生了什么,我在AndroidManifest.xml中添加了 android:requestLegacyExternalStorage =“true” ,
仍然没有变化,我不知道该怎么办?请帮我
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File _fileimage;
final _picker = ImagePicker();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
_fileimage != null
? FileImage(_fileimage)
: Text('No image selected'),
RaisedButton(
onPressed: () => _takePicture,
child: Text('hi'),
)
],
),
),
),
);
}
void _takePicture() async {
final pickedImage = await _picker.getImage(source: ImageSource.gallery);
// if (pickedImage == null) {
// return;
// }
File tmpFile = File(pickedImage.path);
final Directory directory = await getApplicationDocumentsDirectory();
final String path = directory.path;
final String fileName =
basename(pickedImage.path); // Filename without extension
final String fileExtension = extension(pickedImage.path); // e.g.
tmpFile = await tmpFile.copy('$path/$fileName$fileExtension');
setState(() => _fileimage = tmpFile);
}
}
最佳答案
我发现您的代码有问题。
您在“小部件”中使用的“FileImage”是必需的。由于“FileImage”返回“ImageProvider”,因此应使用“Image.file()”而不是“FileImage”。
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File _fileimage;
final _picker = ImagePicker();
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
_fileimage != null
? Image.file(_fileimage)
: Text('No image selected'),
RaisedButton(
onPressed: () async {
_takePicture();
},
child: Text('hi'),
)
],
),
),
),
);
}
void _takePicture() async {
final pickedImage = await _picker.getImage(source: ImageSource.gallery);
// if (pickedImage == null) {
// return;
// }
File tmpFile = File(pickedImage.path);
final Directory directory = await getApplicationDocumentsDirectory();
final String path = directory.path;
final String fileName =
basename(pickedImage.path); // Filename without extension
final String fileExtension = extension(pickedImage.path); // e.g.
tmpFile = await tmpFile.copy('$path/$fileName$fileExtension');
setState(() => _fileimage = tmpFile);
}
}