赞
踩
image_picker: ^0.6.7+12 # 最新版本请在pub.dev中查看
<!-- 在application标签上添加 android:requestLegacyExternalStorage="true" -->
<application android:requestLegacyExternalStorage="true"></application>
<!-- 在application标签后添加权限设置 -->
<uses-permission android:name="android.permission.CAMERA" />
在info.plist中添加配置
NSPhotoLibraryUsageDescription
NSCameraUsageDescription
NSMicrophoneUsageDescription
import 'dart:io'; import 'package:flutter/material.dart'; import 'package:image_picker/image_picker.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { File _image; final picker = ImagePicker(); Future getImage() async { final pickedFile = await picker.getImage(source: ImageSource.camera); setState(() { if (pickedFile != null) { _image = File(pickedFile.path); } else { print('No image selected.'); } }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Image Picker Example'), ), body: Center( child: _image == null ? Text('No image selected.') : Image.file(_image), ), floatingActionButton: FloatingActionButton( onPressed: getImage, tooltip: 'Pick Image', child: Icon(Icons.add_a_photo), ), ); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。