我是“ flutter”的新手,我有疑问。 可以复制图像并将其保存在设备中吗?
File image;
picker() async {
print('Picker is called');
File img = await p.ImagePicker.pickImage(source: p.ImageSource.gallery);
if (img != null) {
image = img;
setState(() {});
}
}
save() async {
print('Save is called');
final Directory path = await image.parent;
setState(() {});
}
我有我的图像,可以获取目录的路径。但是,如何使用其他名称将该图像保存在此目录中? 假设我的图片叫做x.jpg,我希望将同一张图片保存为y.jpg,而不删除x.jpg
答案 0 :(得分:0)
使用simple_permissions插件获得写入外部存储的权限。
使用path_provider插件获取外部存储文件夹的路径
最后使用image插件编写图像
示例代码
Future<Io.File> saveImage(Io.File file) async {
try {
var dir = await getExternalStorageDirectory();
var testdir =
await new Io.Directory('${dir.path}/testfolder').create(recursive: true);
IM.Image image = IM.decodeImage(file.readAsBytesSync());
return new Io.File(
'${testdir.path}/${DateTime.now().toUtc().toIso8601String()}.png')
..writeAsBytesSync(IM.encodePng(image));
} catch (e) {
print(e);
return null;
}
}
还要检查权限
void checkPer() async {
bool checkResult = await SimplePermissions.checkPermission(
Permission.WriteExternalStorage);
if (!checkResult) {
var status = await SimplePermissions.requestPermission(
Permission.WriteExternalStorage);
//print("permission request result is " + resReq.toString());
if (status == PermissionStatus.authorized) {
//saveimage
}
} else {
//saveimage
}
}
命名空间
import 'dart:io' as Io;
import 'package:image/image.dart' as IM;
import 'package:path_provider/path_provider.dart';
import 'package:simple_permissions/simple_permissions.dart';