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

flutter download
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

Recents

Why Is PNG file with Drop Shadow in Flutter Web App Grainy?
How to troubleshoot crashes detected by Google Play Store for Flutter app
Cupertino DateTime picker interfering with scroll behaviour
Why does awk -F work for most letters, but not for the letter "t"?
Flutter change focus color and icon color but not works
How to print and connect to printer using flutter desktop via usb?
Critical issues have been reported with the following SDK versions: com.google.android.gms:play-services-safetynet:17.0.0
Flutter Dart - get localized country name from country code
navigatorState is null when using pushNamed Navigation onGenerateRoutes of GetMaterialPage
Android Sdk manager not found- Flutter doctor error
Flutter Laravel Push Notification without using any third party like(firebase,onesignal..etc)
How to change the color of ElevatedButton when entering text in TextField

Related

How to solve Unhandled Exception: FormatException: Could not find End of Central Directory Record while downloading file in Flutter?
Pdfmake in node.js to download pdf to flutter application
Calling a UI method from Isolate listen method in Flutter throws exception
How can I download a pdf from firebase and access it from my app directory?
How to fix "FileSystemException: Cannot open file" in flutter when reading and writing files?
Download file from url, save to phones storage
Why Is PNG file with Drop Shadow in Flutter Web App Grainy?
How to troubleshoot crashes detected by Google Play Store for Flutter app
Cupertino DateTime picker interfering with scroll behaviour
Flutter change focus color and icon color but not works

两个鬼故事英文名字起名网站起名字打分一百分巴黎圣母院摘抄最好比分网为饭店取名起名大全额首称庆美食大战老鼠修改器小猫起名字高奇今天出生的女孩起什么名字西伯利亚的理发师1992年属猴起名字包姓男孩起什么名字好著名起名专家本草纲目起名一路向西在线看完整版睿与什么字起名大全鸡公煲根据姓名起网名什么是模拟信号嗤之以鼻是什么意思张东健电影开业宣传朋友圈怎么发磨皮软件读者和主角绝壁是真爱安男人起名桌面不显示睿字取名起名大全中华起名网免费姓名打分赘婿出山全文免费阅读少年生前被连续抽血16次?多部门介入两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”淀粉肠小王子日销售额涨超10倍高中生被打伤下体休学 邯郸通报单亲妈妈陷入热恋 14岁儿子报警何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言张家界的山上“长”满了韩国人?男孩8年未见母亲被告知被遗忘中国拥有亿元资产的家庭达13.3万户19岁小伙救下5人后溺亡 多方发声315晚会后胖东来又人满为患了张立群任西安交通大学校长“重生之我在北大当嫡校长”男子被猫抓伤后确诊“猫抓病”测试车高速逃费 小米:已补缴周杰伦一审败诉网易网友洛杉矶偶遇贾玲今日春分倪萍分享减重40斤方法七年后宇文玥被薅头发捞上岸许家印被限制高消费萧美琴窜访捷克 外交部回应联合利华开始重组专访95后高颜值猪保姆胖东来员工每周单休无小长假男子被流浪猫绊倒 投喂者赔24万小米汽车超级工厂正式揭幕黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发当地回应沈阳致3死车祸车主疑毒驾恒大被罚41.75亿到底怎么缴妈妈回应孩子在校撞护栏坠楼外国人感慨凌晨的中国很安全杨倩无缘巴黎奥运校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变王树国卸任西安交大校长 师生送别手机成瘾是影响睡眠质量重要因素国产伟哥去年销售近13亿阿根廷将发行1万与2万面值的纸币兔狲“狲大娘”因病死亡遭遇山火的松茸之乡“开封王婆”爆火:促成四五十对奥巴马现身唐宁街 黑色着装引猜测考生莫言也上北大硕士复试名单了德国打算提及普京时仅用姓名天水麻辣烫把捣辣椒大爷累坏了

两个鬼故事 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化