How to create folder which user able to see in Flutter? - 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?

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 load sqlite file from assets folder in Flutter web?

I usually load the sqlite data file from the assets folder on the Windows and Android platforms by writing bytedata to the file, then look for the path of the file to open it in the database. Like this for example:
sqfliteFfiInit();
factory = databaseFactoryFfi;
dbPath = join((await getTemporaryDirectory()).path, "dictionary.db");
ByteData data = await rootBundle.load('assets/dictionary.sqlite');
List<int> bytes = data.buffer.asUint8List(
data.offsetInBytes,
data.lengthInBytes,
);
await File(dbPath).writeAsBytes(bytes, flush: true);
db = await factory.openDatabase(dbPath);
List<Map> temp = await db.query('word_list');
And now how to load sqlite file data from assets folder, but in flutter web. Meanwhile, on the web platform, you cannot write files.
The libraries I used to create the database sqflite_common_ffi_web: ^0.3.1 and sqflite_common_ffi: ^2.2.0+1

Flutter Write Data in text file shows File Format is not supported in device

generateCsv() async {
String csvData = 'test data';
final String directory = (await getApplicationSupportDirectory()).path;
final path = "$directory/csv-${DateTime.now()}.csv";
print(path);
final File file = File(path);
await file.writeAsString(csvData);
}
in this case file generated but we cant able to open the file . it shows unsupported file format .
we also tried txt file format. it also shows same error. This is a android PDA device and when i connect to the PC the file is not showing in the PC but the interesting thing is when i copy and paste the same file in same location pasted file will open and it will shows in the PC also. My PDA android version is 7.
await file.writeAsString(text, mode: FileMode.append, flush: true);

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 find absolute path fo the "assets" folder of the Flutter app?

Here is the setup in side of my
pubspec.yaml
...
path_provider: ^0.4.1
assets:
- assets/
- assets/a.txt
...
I try to find way to access to the "assets" directory to read content of the text file named "a.txt" where it should read in relative way as "assets/a.txt".
Here is my code in my
main.dart:
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
String fPath = '${appDocDir.parent.path}/assets/a.txt';
new File(fPath).readAsString().then((String contents) {
print("FILE:..... "+contents);
});
And here I got this error:
[VERBOSE-2:shell.cc(184)] Dart Error: Unhandled exception:
FileSystemException: Cannot open file, path = '/var/mobile/Containers/Data/Application/EE2A0E91-6543-4CE3-A82B-3B69BB892BFC/assets/a.txt' (OS Error: No such file or directory, errno = 2)
Seem like the ... EE2A0E91-6543-4CE3-A82B-3B69BB892BFC/ is not the root directory of the app to concat with my file assets/a.txt.
How can I find the exact absolute path of my a.txt file in order to read it using File() class?
You need to use AssetBundle to read assets.
For example https://docs.flutter.io/flutter/services/AssetBundle/load.html or https://docs.flutter.io/flutter/services/AssetBundle/loadString.html
See also https://flutter.io/docs/development/ui/assets-and-images#loading-text-assets