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

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

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;

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();

Use MANAGE_EXTERNAL_STORAGE may is not be a good idea .
You should request the MANAGE_EXTERNAL_STORAGE permission only when your app cannot effectively make use of the more privacy-friendly APIs, such as Storage Access Framework or the Media Store API. Additionally, the app's usage of the permission must fall within permitted uses, and must be directly tied to the core functionality of the app. If your app includes a use case that's similar to the following examples, it's likely to be allowed to request the MANAGE_EXTERNAL_STORAGE permission:
File managers
Backup and restore apps
Anti-virus apps
Document management apps
On-device file search
Disk and file encryption
Device-to-device data migration
https://developer.android.com/training/data-storage/manage-all-files

Related

Flutter, how to create same path directory in Android and IOS

File('/data/user/0/com.abc/cache/full.jpg')
.writeAsBytesSync(class_image.encodeJpg(data));
I found out that Android works with this path. But in ios, I found out that it doesn't work with the error below.
[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.abc/cache/full.jpg' (OS Error: No such file or directory, errno = 2)
I want to use the same folder for both. Is this possible? If not, the workaround is to manage the ios folder separately?
File('/data/user/0/com.abc/cache/full.jpg') I have observed that you have specified path manually. The path you speicified works only for android but for ios it is completely different.So instead of specifying manually use path provider and create platform specified path then use it
import 'package:path_provider/path_provider.dart';
Future<Directory?> getLocalDirectory() async {
return Platform.isAndroid ? await path.getTemporaryDirectory(): await path.getApplicationSupportDirectory(); }
The storage directory of each platform is different
You can obtain the local storage directory separately through the path_provider plugin, and then create your path.
like this
Future<Directory?> getLocalDirectory() async {
return Platform.isAndroid
? await path.getExternalStorageDirectory()
: await path.getApplicationSupportDirectory();
}

How to create folder which user able to see in Flutter?

I tried to create folder with path_provider package with this code.
final Directory directory = await getExternalStorageDirectory();
print("path1="+directory.path );// this is using path_provider
String directoryPath = directory.path + '/bozzetto_camera';
await Directory(directoryPath).create(recursive: true).then((value){
print("path="+value.toString());
});
And in console it shows path=Directory: '/storage/emulated/0/Android/data/com.example.water_delivery_app/files/bozzetto_camera'.
In file manager I could not find any folder.I tried this answer but it did not work I am using a real android device via usb. So how can create directory that user can see?

flutter: how to create/write a file to a specific directory path

i'm trying to create/write a file to a specific directory. i already have my code but the problem is i always got an error saying:
flutter: FileSystemException: Cannot open file, path = '/var/mobile/Containers/Data/Application/AF14244D-E8C4-4B9A-8005-D7CA7CC3520B/Documents/cache/files/abcb-1234567890' (OS Error: No such file or directory, errno = 2)
here's my code:
Directory directory = Platform.isAndroid ? await getExternalStorageDirectory() : await getApplicationDocumentsDirectory();
String path = directory.path + '/cache/files';
File file = new File('$path/$fileID');
final results = await post(...);
file.writeAsBytesSync(results);
what am i missing?
additional question: if anyone would know how can i access created date of the file that i have just created? - reason for this is to delete them after a set date or like an expiry date.
appreciate any help. thank you in advance. =)
Are you sure this path exists?
I belive your code is executing part getApplicationDocumentsDirectory()
That mean you are not on android.
Can you access this path using other app (some file explorer etc)?
The interesting part is here:
'/cache/files' maybe this folder doesn't exist.
Here is on my Windows:
var dir = io.Directory.fromUri(Uri.directory('UNO\\DUE\\'));
dir.createSync(recursive: true);
var file = io.File('${dir.absolute.path}\demo.txt');
file.writeAsStringSync('ABCDEFHJ');
Without dir.createSync(recursive: true); without recursive parameter I have exactly same problem.

How to open a file using its absolute path?

How to open a file using its absolute path?
await File('c:/Users/rozerro/AndroidStudioProjects/rxdart_streams/assets/sonet.txt')
.openRead()
.transform(utf8.decoder)
.transform(LineSplitter())
.toList();
as well as
c:\\Users\\rozerro\\AndroidStudioProjects\\rxdart_streams\\assets\\sonet.txt
The error
E/flutter ( 4943): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)]
Unhandled Exception: FileSystemException: Cannot open file, path =
'c:/Users/rozerro/AndroidStudioProjects/rxdart_streams/assets/sonet.txt'
(OS Error: No such file or directory, errno = 2)
You cannot do that. You cannot access a file on your local PC from an app deployed on your smartphone.
Since your text file is in a folder named assets, I assume it is some kind of file you want to have deployed with your app. In flutter that is called an "asset" and you can read up on how to achieve that in the documentation about assets and images.
In short, your pubspec.yaml file needs to have an entry for it:
flutter:
assets:
- assets/sonet.txt
And then you can access it in your source with this method:
final textFromFile = await rootBundle.loadString('assets/sonet.txt');

security error on getFile call on given directory entry in chrome app

Summary : I am downloading files to chrome app sandbox file system. I have directory called 'downloads' in chrome app's sandbox file system.Following is the code sample
var getSubDirectory = function()
{
subdir.getFile('demo',{create: true},function(fentry)
{
console.log(fentry);
},errohandler)
}
subdir is directory entry. subdir is returning properly, I am getting its output.But when i try to create file into that directory using getFile function, it throws security error.I have permission in manifest for the filesystem.write and filesyste.directory.I tried isWritableEntry() on subdir, its returning false.