how to get the path of download directory in maui? - maui

string location = Path.Combine(FileSystem.Current.AppDataDirectory, "Download", "abc.jpg");
image.Source = ImageSource.FromFile(location);

Your download path of the location is equivalent to below:
Windows:
C:\Users\User\AppData\Local\Packages\{GUID}_{hash}\LocalState\Download
Android:
/data/user/0/your_appliactionID/files/Download
iOS\macOS:
Returns the Library directory that is backed up by iTunes and iCloud.
Official reference link:https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/storage/file-system-helpers?tabs=android

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 solve "OS Error: File exists, errno = 17" in 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

Flutter archive: unzip password-protected folder

In my flutter application I have to download a password-protected zip folder from a remote server and import the .sqlite that is inside in my app.
I'm trying with archive library, but of course I'm open for a better solution if any.
I used the password parameter of ZipDecoder().decodeBytes() method, but it is not working for me.
//inside the `onDone()` callback of an http request
Archive archive = ZipDecoder().decodeBytes(
_myDownloadedData,
password: "mySuperSecretPsw", //here I set the archive psw
);
Uint8List fileDbBytes = Uint8List.fromList(archive.first.content);
setAsWorkingDb(fileDbBytes);
The archive.first.content from the snippet above looks still encrypted. I get the following errors:
E/SQLiteLog(12256): (26) file is encrypted or is not a database
E/DefaultDatabaseErrorHandler(12256): Corruption reported by sqlite on database: /data/user/0/my.own.package/app_flutter/working_data.db
E/DefaultDatabaseErrorHandler(12256): deleting the database file: /data/user/0/my.own.package/app_flutter/working_data.db
You can try
Archive archive = ZipDecoder().decodeBytes(
_myDownloadedData,
verify: true,
password: "mySuperSecretPsw", )