首页 > 解决方案 > 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;
    });}

在此处输入图像描述

标签: dartflutter

解决方案


如果您只想从图库中选择,则可以使用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),
      ),
    );
  }
}

推荐阅读