How to solve "OS Error: File exists, errno = 17" in flutter?

1,062

Solution 1

Apparently seems to be a problem with the way Android deletes files: the first time it works, but the next times the error compare, even if I delete manually the file.

My Solution: append a random string to file's name. I know that it's not so elegant, but it works.

 String filePath =
        _localPath + "/" + fileName + Uuid().v4() + "." + extension;

Solution 2

I found the solution: Since Android 11, API level 30, there is a new storage protection. You need to request the MANAGE_EXTERNAL_STORAGE access.


Solution

Add to AndroidManifest.xml

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

And request Permission to manage external Storage (example with permission_handler)

await Permission.manageExternalStorage.request();
Share:
1,062
Emanuele Vinci
Author by

Emanuele Vinci

Updated on December 29, 2022

Comments

  • Emanuele Vinci
    Emanuele Vinci over 1 year

    I'm trying to download and save a file in the Downlaod directory, but every time I get this error:

    I/flutter (21400): FileSystemException: Cannot create file, path = '/storage/emulated/0/Download/contratto.jpg' (OS Error: File exists, errno = 17)
    

    But if I search this file in my storage I don't find it, I don't understand.

    In the Manifest I've all the permissions:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    I'm using ext_storage package to find the path, but even with path_provider I got the same error. This is my code:

    static Future download(String url, String fileName, String extension) async {
        _checkPermission();
        String _localPath = await ExtStorage.getExternalStoragePublicDirectory(
            ExtStorage.DIRECTORY_DOWNLOADS);
        Dio dio = Dio();
        await dio.download(
          url,
          _localPath + "/" + fileName + "." + extension,
        );
    }
    

    I'm using Flutter 2.0.6. Any ideas?

    EDIT: The download works only the first time I save a file with a certain name, if I delete it and try to download it again I get this error. I've tried also to reinstall the app but I still get the error

  • Ahmed Nabil
    Ahmed Nabil almost 3 years
    It worked, but do you know any more information about this error? what's the cause of it for example.
  • Emanuele Vinci
    Emanuele Vinci almost 3 years
    I don't know for sure, the error appears the second time you try to save a file with the same name (the first time you don't get the error), even if you delete the file. My theory is that there is a sort of cache or something that cause the problem.
  • Ahmed Nabil
    Ahmed Nabil almost 3 years
    Yes, that's what happens. very weird. Thanks anyways.
  • Kishan Dhankecha
    Kishan Dhankecha over 2 years
    This answer did not provide solution in my case. I am checking if file exists then I opens the file using open_file plugin. In my case File.exist(file) returns false but dio.download showing this same error: OS Error: File exists, errno = 17
  • Kishan Dhankecha
    Kishan Dhankecha over 2 years
    I am also checking the file size from Dio().head(fileUrl) and checking this value with the Local file. If somehow download stopped in middle and file did not delete automatically this above condition will delete the corrupted file and download again. As I am getting File.exist(file) as false, I can't compare the size and I have to download the file. which as mentioned throws error OS Error: File exists, Errno = 17