1

If the image_picker library return the exist file path, how to directly change it?

void getImage(ImageSource imageSource) async {
  final pickedFile = await ImagePicker().pickImage(source: imageSource);
  if (pickedFile != null) {
    pickedFile.path; // how to rename?
  }
}
ccd
  • 5,788
  • 10
  • 46
  • 96

1 Answers1

2

Use path package.

import 'package:path/path.dart' as path;

Then create a new target path to rename the file.

File pickedFile = await ImagePicker().pickImage(source: imageSource);
  if (pickedFile != null) {
    pickedFile.path; // how to rename?
  }

String dir = path.dirname(pickedFile.path);
String newName = path.join(dir, 'what ever name you want.jpg');
pickedFile.renameSync(newName);
Reham Alraee
  • 139
  • 8