How can I save a local file into assets folder in Flutter - flutter

My flutter desktop application takes a local file (audio/video/image) as user input. Now I need to save it to somewhere for later usage. So that even if the device changes the application can still access them using relative path.
What I want to do is copy this file to assets folder using file.copy method. But how can I get the assets folder's directory? I don't want to hardcode assets folder's absolute path. Is there any way to copy files into assets folder using relative path?
Or any alternate way to do the task?

assets is read-only. Use documents directory where the application can store files that only it can access.
final directory = await getApplicationDocumentsDirectory();
See Read and write files for details.

Related

Flutter - Where should I save downloaded files?

I am building a flutter app (iOS/Android/MacOS/Windows/Web) which is able to download files, selected by the user. But where should I save those files? On MacOS/Windows its easy using path_provider and get the path of the downloads folder.
But where should I save the files on the other platforms? The downloads folder would be perfect because the users are able to use those files (.pdf) even without using the app, so the files are not app specific.
If you wish to save in download directory you can use
Directory appDownloadDir = await getDownloadsDirectory();
String appDownloadPath = appDownloadDir.path;
Or
getExternalStorageDirectory()//android
getApplicationDocumentsDirectory()// ios doesnt support access to download folder

How do I access the an assets subdirectory with Directory in Flutter

I am working on an app in Flutter where I will have to find filenames that start with specific keywords in a directory (and then do things with those filenames subsequently). However, I am encountering trouble accessing the directory itself.
I have an 'assets' folder in the root directory of my project, as many online sources recommended, and a subdirectory in that directory. I include this subdirectory in my pubspec.yaml in the format 'assets/[subdirectory name]', and can generally access its contents with 'assets/[subdirectory name]/[filename]' with methods like loadString. However, when I try to access the directory like this: Directory('assets/subdirectory/'), a FileSystemException no such directory error is thrown. I have tried the usual combination of adding './' or removing the final slash, to no avail.
Are Directory filepaths relative to something else? Is there a better way to do this? Any help would be appreciated.
Directory() operates on the filesystem. You can't access assets with it because assets are bundled with the app during the build process.
During a build, Flutter places assets into a special archive called the asset bundle that apps read from at runtime.
https://docs.flutter.dev/development/ui/assets-and-images#asset-bundling
To get a list of assets in the app, you can use the undocumented generated file AssetManifest.json which contains a map of all bundled assets:
final assetsManifest = await DefaultAssetBundle.of(context).loadString('AssetManifest.json');
final assets = json.decode(assetsManifest).keys;
Another option (perhaps cleaner, since AssetsManifest.json is AFAIK indeed undocumented and might change in future versions) would be to simply make your own list of assets and save it as another asset (JSON file).

Flutter Dart File path

quick Flutter Dart issue.
I am trying to read from a text file located inside the app's directory, however Flutter does not recognize a single file path that I provide it. The file path is always responded to with "OS Error: No such file or directory, errno = 2".
I am trying to read from a file located in assets/importfiles, from a dart class inside lib/viewmodels. The assets directory has already been added to pubsec.yaml.
Here is the picture of my directory structure
I can even have the main class locate and print out the files' paths using rootBundle, but trying to initialize a new File using those paths provides the same error text.
If I were to use path provider to locate and read from files, how would I initially put the files into the Temporary Directory or Application Documents Directory, if Flutter does not recognize the file paths of the files that I want to insert?
Just You need to remove folder name ''.
assets\importfiles
to
assets Or importfiles
Thanks You.Happy To help you
Have you tried putting a "/" before "assets" in the file path?
I'm not sure that this will fix the error, let me know if it works!

How to copy file from applicationStorageDirectory into dataDirectory?

I would like to copy an image file located in www/assets/imgs/christmas.jpg to the dataDirectory folder using the Ionic Native File Plugin, however I don't know how to access the www folder. I tried the following:
this.file.checkFile(this.file.applicationStorageDirectory, 'assets/imgs/christmas.jpg')
But it always returns NOT_FOUND_ERR.
You should change applicationStorageDirectory to applicationDirectory (although I am not sure what the difference is) and add www in front of the file path as the application directory contains the www folder and not its contents. So instead of assets/imgs/christmas.jpg use www/assets/imgs/christmas.jpg

open and read zip file from chrome-app

Now that google chrome handles zip files, basically, is there a way from inside a chrome app to get the files, and the contents of the files in a zip archive? From the user end, a zip file is mounted as a drive, containing the contents, but from the app end, the zip file is just a file. Is there a way from the app to "mount" the file, get the mount point and enumerate the contents and inflate them?
Nope, there are no particular APIs exposed that would allow this.
You will need to include your own ZIP engine - be it in JavaScript or in (P)NaCl. Then you can work with HTML Filesystem to hold inflated files while you work with them.