dart - Flutter-如何打开手机相册?
问题描述
我想在这里打开画廊目录,我正在使用此代码,但显示了我想打开唯一画廊的所有选项。
List images;
int maxImageNo = 10;
bool selectSingleImage = false;
File _imageFile;
_pickImageFromGallery() async {
File file;
String result;
try {
result = await FlutterImagePickCrop.pickAndCropImage(_gallery);
} on PlatformException catch (e) {
result = e.message;
print(e.message);
}
if (!mounted) return;
setState(() {
imageFile = new File(result);
_platformMessage = result;
});}
解决方案
如果您只想从图库中选择,则可以使用image_picker插件。如果裁剪对您很重要,那么我会重新考虑我的答案,因为image_picker
它没有提供。
import 'package:image_picker/image_picker.dart';
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File _image;
Future getImage() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_image = image;
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Image Picker Example'),
),
body: new Center(
child: _image == null
? new Text('No image selected.')
: new Image.file(_image),
),
floatingActionButton: new FloatingActionButton(
onPressed: getImage,
tooltip: 'Pick Image',
child: new Icon(Icons.add_a_photo),
),
);
}
}
推荐阅读
- java - 如何在 JNA 结构中映射联合
- amazon-web-services - 如何修改亚马逊 AWS EC2 启动模板?
- javascript - Angular PWA - 缓存远程图像
- react-native - Flex box 不会导致本机反应?
- postgresql - 数据库中的迁移是否会自动反映在只读副本中?
- r - 在 R 中向 hunspell 词典添加自定义单词的问题
- html - 按钮未扩展以填充表格单元格
- c - 为什么我不能将 FS 寄存器的 PID 块的值分配给 unsigned int?
- android-recyclerview - GridLayoutManager 项目宽度 wrap_content 不起作用
- javascript - 如何在 Java 中使用 HtmlUnit 从页面获取“发布日期”动态值?