简介
项目中使用了3个关于选取照片的插件,这里简单记录一下。
1. 从相册选取照片
- 这个插件热度比较高
- 只有从相册选取照片的功能,没有拍照的功能
- 名字叫文件选取,适用范围比单纯的取照片广
- 可以配置成取视频
- 文件可以选一张,也可以选多张,有参数配置
- 可以进一步封装成静态函数工具,方便使用
/// 选择1张图片,返回图片的本地路径
static Future<String> pickOneImage() async {
String imgPath = "";
FilePickerResult? result;
try {
result = await FilePicker.platform.pickFiles(
type: FileType.image,
allowMultiple: false,
onFileLoading: (FilePickerStatus status) => LogUtil.log(status),
allowedExtensions: null,
);
} on PlatformException catch (e) {
LogUtil.log("Unsupported operation: $e");
} catch (ex) {
LogUtil.log(ex);
}
if (result != null) {
List paths = result.paths;
String first = paths[0];
if (first.isNotEmpty) {
imgPath = first;
}
}
return imgPath;
}
2. 选图片和拍照1
- 能选照片,也能拍照
- 照片和拍照通过不同的参数来指定
- 封装成静态函数
static Future<List> pickImageFromGallery() async {
List picList = [];
try {
final picker = ImagePicker();
final pickedFile = await picker.pickMultiImage(limit: 9);
if (pickedFile.isNotEmpty) {
for (XFile file in pickedFile) {
if (file.path.isNotEmpty) {
picList.add(file.path);
}
}
return picList;
} else {
return [];
}
} catch (e) {
return [];
}
}
static Future<String> pickImageFromCamera() async {
try {
final picker = ImagePicker();
final pickedFile = await picker.pickImage(source: ImageSource.camera);
if (pickedFile != null) {
return pickedFile.path;
}
return "";
} catch (e) {
return ''";
}
}
- 问题:据说取照片的时候,限制9张照片限制不了。
3. 选图片和拍照2
- 能选照片,也能拍照
- 照片和拍照通过不同的参数来指定
- 封装成静态函数
- 热度不高,但是可以有效限制照片数量
static Future<List> newPickImageFromGallery() async {
List picList = [];
try {
final picker = HLImagePicker();
final pickedFile = await picker.openPicker(
pickerOptions: const HLPickerOptions(maxSelectedAssets: 9, mediaType: MediaType.image, usedCameraButton: false),
localized: const LocalizedImagePicker(
noAlbumPermissionText: '无权限访问相册',
noCameraPermissionText: '无权限访问相机',
maxSelectedAssetsErrorText: '超过所选项目的最大数量',
doneText: '完成',
cancelText: '取消',
loadingText: '加载中',
tapHereToChangeText: '点击更改',
emptyMediaText: '无媒体',
gifErrorText: 'GIF不支持',
okText: '确定',
cropCancelText: '取消',
cropDoneText: '完成',
cropTitleText: '裁剪'));
if (pickedFile.isNotEmpty) {
for (HLPickerItem file in pickedFile) {
if (file.path.isNotEmpty) {
picList.add(file.path);
}
}
return picList;
} else {
return [];
}
} catch (e) {
return [];
}
}
static Future<String> newPickImageFromCamera() async {
try {
final picker = HLImagePicker();
final pickedFile = await picker.openCamera();
if (pickedFile != null) {
return pickedFile.path;
}
return '';
} catch (e) {
return '';
}
}
- 可以进一步封装成一个底部弹窗sheet,方便使用
/// 弹出sheet弹框提示用户使用相机还是相册选择照片
/// 回调得到图片的本地路径
static Future<List> showImagePickerSheet() async {
List imageUrls = [];
try {
// 弹出sheet弹框提示用户使用相机还是相册选择照片
await showCupertinoModalPopup(
context: Get.context!,
builder: (context) {
return CupertinoActionSheet(
actions: <Widget>[
CupertinoActionSheetAction(
child: const Text('拍照'),
onPressed: () async {
String imgUrl = await newPickImageFromCamera();
imageUrls.add(imgUrl);
Get.back();
},
),
CupertinoActionSheetAction(
child: const Text('从相册选择'),
onPressed: () async {
imageUrls = await newPickImageFromGallery();
Get.back();
},
),
],
cancelButton: CupertinoActionSheetAction(
child: const Text('取消'),
onPressed: () {
Get.back();
},
),
);
},
);
} catch (e) {
print(e);
imageUrls = [];
}
return imageUrls;
}