pickImagesToFile static method

Future<List<File>?> pickImagesToFile(
  1. BuildContext context, {
  2. bool usingCamera = true,
  3. bool usingGallery = true,
  4. bool allowMultiple = true,
  5. int? maxSize,
})

Implementation

static Future<List<File>?> pickImagesToFile(
  BuildContext context, {
  bool usingCamera = true,
  bool usingGallery = true,
  bool allowMultiple = true,
  int? maxSize,
}) async {
  List<ResultItem>? images = await _pickImages(
    context,
    usingCamera: usingCamera,
    usingGallery: usingGallery,
    allowMultiple: allowMultiple,
    maxSize: maxSize,
  );

  if (images == null) return null;

  List<File> files = [];

  for (ResultItem item in images) {
    File file = File.fromUri(Uri.parse(item.filePath));
    bool fileExists = await file.exists();

    if (!fileExists) {
      final data = await _readFileByte(item.filePath);
      final buffer = data.buffer;
      final Directory extDir = await getApplicationDocumentsDirectory();
      final String dirPath = '${extDir.path}/Pictures/flutter_test';
      await Directory(dirPath).create(recursive: true);
      final String filePath = '$dirPath/${_timestamp()}.jpg';
      file = await File(filePath).writeAsBytes(buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
    }

    files.add(file);

    if (item.albumId == null) {
      File cameraImage = File(item.filePath);

      bool fileExists = await cameraImage.exists();

      if (!fileExists) {
        cameraImage.deleteSync();
      }
    }
  }

  return files;
}