app直播系统源码,Flutter拍照与选择照片并且保存到本地
app直播系统源码,Flutter拍照与选择照片并且保存到本地实现的相关代码
核心代码:
final picker = ImagePicker(); //初始化
///存放的图片
File _imgPath;
Future getImage(bool isTakePhoto) async {
try {
var pickedFile = await picker.getImage(
source: isTakePhoto ? ImageSource.camera : ImageSource.gallery);
if (pickedFile != null) {
setState(() {
_imgPath = File(pickedFile.path);
});
} else {
print('没有选择任何图片');
}
} catch (e) {
print("该手机不支持相机");
}
}
具体代码:
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
class ImageTest extends StatefulWidget {
@override
State<StatefulWidget> createState() => ImageState();
}
class ImageState extends State<ImageTest> {
final picker = ImagePicker();
File _imgPath;
Future getImage(bool isTakePhoto) async {
try {
var pickedFile = await picker.getImage(
source: isTakePhoto ? ImageSource.camera : ImageSource.gallery);
if (pickedFile != null) {
setState(() {
_imgPath = File(pickedFile.path);
});
} else {
print('没有选择任何图片');
}
} catch (e) {
print("该手机不支持相机");
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: [
MaterialButton(
minWidth: 100,
height: 100,
color: Colors.blue,
onPressed: () {
getImage(true);
},
child: Text(
"拍照",
style: TextStyle(fontSize: 32, color: Colors.white),
)),
SizedBox(
height: 30,
),
MaterialButton(
minWidth: 100,
height: 100,
color: Colors.blue,
onPressed: () {
getImage(false);
},
child: Text("选择照片",
style: TextStyle(fontSize: 32, color: Colors.white))),
_imgPath != null
? Image.file(
_imgPath,
fit: BoxFit.cover,
)
: Center(
child: Text(
"没有选择照片",
style: TextStyle(fontSize: 32, color: Colors.black)s,
))
],
),
);
}
}
注意:此处保存的图片存放在缓存中,如果需要保存到本地则需使用image_gallery_saver,用于保存。
以上就是 app直播系统源码,Flutter拍照与选择照片并且保存到本地实现的相关代码,更多内容欢迎关注之后的文章