插件
//保存插件
image_gallery_saver
//文件路径插件
path_provider
//打开相册插件
image_picker
选取照片
Future getImage() async {
final pickedFile = await picker.pickImage(source: ImageSource.gallery);
print("pickedFile : ${pickedFile?.path}");
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
print("_image : ${_image}");
} else {
print('No image selected.');
}
});
return pickedFile?.path;
}
选取视频
Future _getVideo() async {
File? videoFile;
final pickedFile = await picker.pickVideo(
source: ImageSource.gallery, maxDuration: const Duration(seconds: 60));
setState(() {
if (pickedFile != null) {
videoFile = File(pickedFile.path);
print("_image : ${_image}");
} else {
print('No image selected.');
}
});
return videoFile;
}
拍摄照片
_takePhoto() async {
final pickedFile =
await picker.pickImage(source: ImageSource.camera, imageQuality: 50);
print("pickedFile : ${pickedFile?.path}");
setState(() {
if (pickedFile != null) {
_image = File(pickedFile.path);
print("_image : $_image");
print(pickedFile);
} else {
print('No image selected.');
}
});
}
拍照视频
Future _takeVideo() async {
File? videoFile;
final pickedFile = await picker.pickVideo(
source: ImageSource.gallery, maxDuration: const Duration(seconds: 60));
setState(() {
if (pickedFile != null) {
videoFile = File(pickedFile.path);
print("_image : ${_image}");
} else {
print('No image selected.');
}
});
}
保存和缓存 图片,视频
class SaveVideo{
//网络照片保存
static getHttp( String url,{
bool isShow = true}) async {
EasyLoading.show(
maskType: EasyLoadingMaskType.black);
var response = await Dio().get(
url ,options: Options(responseType: ResponseType.bytes));
// print("response.data:${response.data}");
// print("response:${response}");
final result = await ImageGallerySaver.saveImage(
Uint8List.fromList(response.data),
quality: 60,
isReturnImagePathOfIOS: true,
);
print("result:$result");
if(isShow){
if (result["isSuccess"]) {
//这里是国际化提示代码,去掉.tr就行,后面带.tr的都是一个道理
EasyLoading.showSuccess("save_successfully".tr);
} else {
EasyLoading.showError("fail_to_save".tr);
}
}
return result["filePath"].toString().substring(8);
或者
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
/// 保存的图片数据. 缓存数据到临时路径,这里也可以缓存视频
Uint8List imageBytes;
CachedNetworkImage image = CachedNetworkImage(imageUrl: url);
BaseCacheManager manager = image.cacheManager ?? DefaultCacheManager();
Map<String, String>? headers = image.httpHeaders;
File file = await manager.getSingleFile(
image.imageUrl,
);
imageBytes = await file.readAsBytes();
/// 保存图片
final results = await ImageGallerySaver.saveImage(imageBytes);
}
# 視頻保存
static saveVideo(BuildContext context,String videoUrl,{
bool isShow = true}) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
EasyLoading.show(
maskType: EasyLoadingMaskType.black);
if(isShow){
///保存
EasyLoading.show(
maskType: EasyLoadingMaskType.black);
var appDocDir = await getTemporaryDirectory();
String savePath =
"${
appDocDir.path}+${
const Uuid().v4()}/${
DateTime.now().millisecondsSinceEpoch}.mp4";
await Dio().download(videoUrl, savePath);
final result = await ImageGallerySaver.saveFile(savePath);
if (result["isSuccess"]) {
EasyLoading.dismiss();
int ? launchTimes = prefs.getInt("launchTimes");
if(launchTimes == 2){
Launch.launchShowCupertinoAlertDialogTwo(context);
}
else{
EasyLoading.showSuccess("save_successfully".tr,);
}
} else {
EasyLoading.showError("fail_to_save".tr);
}
}else
{
/// 保存的图片数据 分享使用
CachedNetworkImage image = CachedNetworkImage(imageUrl: videoUrl);
BaseCacheManager manager = image.cacheManager ?? DefaultCacheManager();
File file = await manager.getSingleFile(
image.imageUrl,
);
EasyLoading.dismiss();
return file.path;
}
}