copyFileIntoDownloadFolder function

Future<bool?> copyFileIntoDownloadFolder(
  1. String filePath,
  2. String fileName, {
  3. File? file,
  4. String? desiredExtension,
})

Copies the file to the download folder with the specified file name and ensures a unique name to avoid overwriting existing files.

  • filePath The path to the source file that needs to be copied.

  • fileName The name for the copied file in the download folder.

  • file (Optional) The File object representing the source file. If not provided, a File object will be created from filePath.

  • desiredExtension (Optional) The desired file extension for the copied file. If not provided, the extension will be derived from the source file's path.

return A Future that completes with a bool value indicating whether the file copy operation was successful or not. Returns true if successful, otherwise returns false.

throws Exception If an error occurs during the file copy operation.

remarks: This method checks the Android SDK version and utilizes a workaround to avoid using MANAGE_EXTERNAL_STORAGE on Android 29 and higher. For devices with Android API 29 and higher, the method uses the platform-specific channel to invoke a native method ('saveFileUsingMediaStore') and saves the file using MediaStore to bypass the restriction.

On devices with Android versions below 29, the method gets the path to the download folder and copies the file using the copyTo method. If the destination file already exists, a unique name is generated by appending a suffix in the form of '_(copyNumber)' to the file name. The copy operation is retried until a unique name is found.

Implementation

Future<bool?> copyFileIntoDownloadFolder(String filePath, String fileName,
        {File? file, String? desiredExtension}) =>
    DownloadsfolderPlatform.instance.copyFileIntoDownloadFolder(
        filePath, fileName,
        file: file, desiredExtension: desiredExtension);