2

I am using the following code snippet to attempt to retrieve the modified time of a file:

import 'dart:io';
import 'package:image_picker/image_picker.dart';

final picker = ImagePicker();
final pickedFile = await picker.getVideo(source: ImageSource.gallery);
final file = File(pickedFile.path);
print("file modified time: ${file.lastModifiedSync().toIso8601String()}");

Whenever I run the the above snippet, regardless of the file, it prints the current dateTime as opposed to the file's modified dataTime

Amy
  • 399
  • 5
  • 12

4 Answers4

2

The way the image_picker work is it creates a temporary copy of the file which you are about to upload in your app. With this implementation, the "current date" you are mentioning is the logically the file's "last modified date" (or creation date). So for now, you may not be able to retrieve it using this plugin.

However, with Flutter's Platform Channels, you should be able to retrieve the file's attributes, eg. for Android, using BasicFileAttributes in native code.

For example in Android:

File file = ...;
BasicFileAttributes attr = Files.readAttributes(file.toPath(), BasicFileAttributes.class);
long lastModifiedAt = attr.lastModifiedTime();

Then using platform channels, you can pass the data like this to Flutter:

result.success(lastModifiedAt)

References

Joshua de Guzman
  • 2,063
  • 9
  • 24
0

I don't believe this information is available, unfortunately (at least on Android, not sure about iOS)

Pierre
  • 308
  • 1
  • 3
  • 9
0

Tested using the file_picker package, hope that can help up with the image_picker.

You can use the cross_file package.


FilePickerResult? result = await FilePicker.platform.pickFiles();

final resultFile = result.files.first;
final resultFilePath = resultFile.path;

if (resultFilePath == null) {
  throw EntityPathException();
}

/// Class from cross_file package
final xfile = XFile(resultFilePath);

final lastModified = await xfile.lastModified();
Thiago Carvalho
  • 360
  • 2
  • 7
0

You can use multi_image_picker package instead. The creation time of the selected image(s) can be accessed through the MetaData class as indicated below:

    List<Asset> images = await MultiImagePicker.pickImages(maxImages: 10, enableCamera: true);

    Metadata metadata = await images[0].metadata;
    DateTime takenOn = metadata.exif.dateTimeOriginal;
Charles Van Damme
  • 765
  • 2
  • 6
  • 14