renameCapturedImage function

Future<File> renameCapturedImage(
  1. File originalFile
)

Implementation

Future<File> renameCapturedImage(File originalFile) async {
  _imageCounter++;
  // Get the current date and format it as desired.
  DateTime now = DateTime.now();
  String formattedDate = DateFormat('yyyyMMdd_HHmm').format(now);

  // Read the file bytes and compute an MD5 hash.
  List<int> fileBytes = await originalFile.readAsBytes();
  // Compute the full hash and then take the first 6 characters.
  String fullHash = md5.convert(fileBytes).toString();
  String shortHash = fullHash.substring(0, 4);

  // Create the new file name.
  String newFileName = '${formattedDate}_${_imageCounter}${shortHash}.jpg';

  // Construct the new file path (same directory as the original).
  String newPath =
      '${originalFile.parent.path}${Platform.pathSeparator}$newFileName';

  // Rename (or move) the file.
  return originalFile.rename(newPath);
}