Rename a file/Image in flutter
First import the path package.
import 'package:path/path.dart' as path;
Then create a new target path to rename the file.
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);
Manish Raj's answer isn't working for me since image_picker: ^0.6.7
, they recently changed their API when getting an image or video which returns PickedFile
instead of File
.
The new method I used was to convert from PickedFile
to File
then copy it over to applications directory with a new name. This method requires 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);
I know the question states that they do not want to move it to a new folder but this was the best way I could find to make the rename work.