在 flutter 中重命名 file/Image
Rename a file/Image in flutter
我正在使用 image_picker 从 gallery/taking 一张照片中挑选一张图片:^0.6.2+3 包。
File picture = await ImagePicker.pickImage(
maxWidth: 800,
imageQuality: 10,
source: source, // source can be either ImageSource.camera or ImageSource.gallery
maxHeight: 800,
);
我得到 picture.path 作为
/Users/[一些路径]/tmp/image_picker_A0EBD0C1-EF3B-417F-9F8A-5DFBA889118C-18492-00001AD95CF914D3.jpg
现在我想把图片重命名为case01wd03id01.jpg
Note : I don't want to move it to new folder
如何重命名?我在官方文档中找不到它。
先导入路径包
import 'package:path/path.dart' as path;
然后创建一个新的目标路径来重命名文件。
File picture = await ImagePicker.pickImage(
maxWidth: 800,
imageQuality: 10,
source: ImageSource.camera,
maxHeight: 800,
);
print('Original path: ${picture.path}');
String dir = path.dirname(picture.path);
String newPath = path.join(dir, 'case01wd03id01.jpg');
print('NewPath: ${newPath}');
picture.renameSync(newPath);
isn't working for me since image_picker: ^0.6.7
,他们最近在获取 returns PickedFile
而不是 File
.
的图像或视频时更改了 API
我使用的新方法是将 PickedFile
转换为 File
,然后使用新名称将其复制到应用程序目录。此方法需要 path_provider.dart
:
import 'package:path_provider/path_provider.dart';
...
...
PickedFile pickedFile = await _picker.getImage(source: ImageSource.camera);
// Save and Rename file to App directory
String dir = (await getApplicationDocumentsDirectory()).path;
String newPath = path.join(dir, 'case01wd03id01.jpg');
File f = await File(pickedF.path).copy(newPath);
我知道问题是他们不想将其移动到新文件夹,但这是我能找到的使重命名生效的最佳方法。
使用此功能只重命名文件名而不改变文件路径。您可以在有或没有 image_picker.
的情况下使用此功能
import 'dart:io';
Future<File> changeFileNameOnly(File file, String newFileName) {
var path = file.path;
var lastSeparator = path.lastIndexOf(Platform.pathSeparator);
var newPath = path.substring(0, lastSeparator + 1) + newFileName;
return file.rename(newPath);
}
在 file.dart 中阅读有关您的 dart SDK 的更多文档。
希望能帮到你,关注
Import path package
import 'package:path/path.dart' as path;
Code below
final file =File("filepath here"); //your file path
String dir = path.dirname(file.path); //get directory
String newPath =path.join(dir, 'new file name'); //rename
print(newPath); //here the newpath
file.renameSync(newPath);
这对我有用
String dir = (await getApplicationDocumentsDirectory()).path;
String newPath = path.join(dir,(DateTime.now().microsecond.toString()) + '.' + file.path.split('.').last);
File f = await File(file.path).copy(newPath);
我正在使用 image_picker 从 gallery/taking 一张照片中挑选一张图片:^0.6.2+3 包。
File picture = await ImagePicker.pickImage(
maxWidth: 800,
imageQuality: 10,
source: source, // source can be either ImageSource.camera or ImageSource.gallery
maxHeight: 800,
);
我得到 picture.path 作为
/Users/[一些路径]/tmp/image_picker_A0EBD0C1-EF3B-417F-9F8A-5DFBA889118C-18492-00001AD95CF914D3.jpg
现在我想把图片重命名为case01wd03id01.jpg
Note : I don't want to move it to new folder
如何重命名?我在官方文档中找不到它。
先导入路径包
import 'package:path/path.dart' as path;
然后创建一个新的目标路径来重命名文件。
File picture = await ImagePicker.pickImage(
maxWidth: 800,
imageQuality: 10,
source: ImageSource.camera,
maxHeight: 800,
);
print('Original path: ${picture.path}');
String dir = path.dirname(picture.path);
String newPath = path.join(dir, 'case01wd03id01.jpg');
print('NewPath: ${newPath}');
picture.renameSync(newPath);
image_picker: ^0.6.7
,他们最近在获取 returns PickedFile
而不是 File
.
我使用的新方法是将 PickedFile
转换为 File
,然后使用新名称将其复制到应用程序目录。此方法需要 path_provider.dart
:
import 'package:path_provider/path_provider.dart';
...
...
PickedFile pickedFile = await _picker.getImage(source: ImageSource.camera);
// Save and Rename file to App directory
String dir = (await getApplicationDocumentsDirectory()).path;
String newPath = path.join(dir, 'case01wd03id01.jpg');
File f = await File(pickedF.path).copy(newPath);
我知道问题是他们不想将其移动到新文件夹,但这是我能找到的使重命名生效的最佳方法。
使用此功能只重命名文件名而不改变文件路径。您可以在有或没有 image_picker.
的情况下使用此功能import 'dart:io';
Future<File> changeFileNameOnly(File file, String newFileName) {
var path = file.path;
var lastSeparator = path.lastIndexOf(Platform.pathSeparator);
var newPath = path.substring(0, lastSeparator + 1) + newFileName;
return file.rename(newPath);
}
在 file.dart 中阅读有关您的 dart SDK 的更多文档。 希望能帮到你,关注
Import path package
import 'package:path/path.dart' as path;
Code below
final file =File("filepath here"); //your file path
String dir = path.dirname(file.path); //get directory
String newPath =path.join(dir, 'new file name'); //rename
print(newPath); //here the newpath
file.renameSync(newPath);
这对我有用
String dir = (await getApplicationDocumentsDirectory()).path; String newPath = path.join(dir,(DateTime.now().microsecond.toString()) + '.' + file.path.split('.').last); File f = await File(file.path).copy(newPath);