14

I am picking an image from gallery/taking a photo using the image_picker: ^0.6.2+3 package.

File picture = await ImagePicker.pickImage(
  maxWidth: 800,
  imageQuality: 10,
  source: source, // source can be either ImageSource.camera or ImageSource.gallery
  maxHeight: 800,
);

And I get picture.path as

/Users/[some path]/tmp/image_picker_A0EBD0C1-EF3B-417F-9F8A-5DFBA889118C-18492-00001AD95CF914D3.jpg

Now I want to rename the image to case01wd03id01.jpg.

Note: I don't want to move it to new folder

How can I rename it? I could not find it in the official documentation.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
L Uttama
  • 379
  • 1
  • 4
  • 14
  • Cant you use picture.renameSync(string newPath)? As mentioned in offical docs: https://api.flutter.dev/flutter/dart-io/File/renameSync.html – DoobieAsDave Jan 24 '20 at 12:29

6 Answers6

14

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
  • 4,903
  • 1
  • 29
  • 39
  • 1
    Cannot rename file to '/var/mobile/Containers/Data/Application/66BC8D60-EF60-4DB2-A267-583A865D494E/Library/Caches/libCachedImageData/case01wd03id01.jpg', path = '/var/mobile/Containers/Data/Application/66BC8D60-EF60-4DB2-A267-583A865D494E/Library/Caches/libCachedImageData/23f98b30-8f13-11ec-bff1-03432be54c34.bin' (OS Error: No such file or directory, errno = 2) – Akbar Pulatov Feb 16 '22 at 10:34
8

Use this function to rename the file only without changing the path of the file. You can use this function with or without 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);
}

Read more documentation in file.dart on your Dart SDK.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Denis Ramdan
  • 1,763
  • 3
  • 18
  • 35
5

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 and then copy it over to the 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
1

This works for me

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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

Import the path package:

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

Code

 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 is the newpath
 file.renameSync(newPath);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rahul Raj
  • 1,010
  • 9
  • 10
0

For anyone who has not had luck with the above options

final picture = await ImagePicker.pickImage(
  maxWidth: 800,
  imageQuality: 10,
  source: source, // source can be either ImageSource.camera or ImageSource.gallery
  maxHeight: 800,
);

final extension = image!.path.split('.').last;
final newFile = File('${Directory.systemTemp.path}/new_name_here.$ext');

await image.saveTo(newFile.path);
print("New path: ${newFile.path}");

return newFile;

newFile will be hold the image with the new name. Note: picture is a final instead of a File.

iampapagray
  • 53
  • 1
  • 6