Store files from flutter application to external storage - flutter

Is it always safe to use the path "/storage/emulated/0/{MY APP NAME}" to store the files ?
I Want to store files in the external storage of the android device and I want to store it in a folder named after the app name this folder needs to be located in the external storage.

Have you tried the package path_provider https://pub.dev/packages/path_provider/install?
It comes with the methods getExternalStorageDirectory() and getApplicationDocumentsDirectory() which might be what you are looking for. As far as I know different os types are automatically considered as well.

try the same package of ext_storage named android_external_storage its the same concept putting the downloaded data for example to the download folder.

Check this thread.
The directory an app can access on sdcard looks like /storage/1718-0217/Android/data/com.example.app_name/files. You can get this with the code below.
(await getExternalStorageDirectories())?[1].path;
One app should only access some restricted directories on sdcard. Or you should use Permission.manageExternalStorage.request(), it is strongly restricted and always return false.
Fortunately, per-app directories can also be scanned by other services such like media.

Related

What is the difference between getTemporaryDirectory() and getApplicationSupportDirectory()?

What is the main difference between await getTemporaryDirectory();
and await getApplicationSupportDirectory() with Flutter Path provider package.
It is mentioned that what is the underlying calls made for the function:
getTemporaryDirectory , getApplicationSupportDirectory
Considering the example of Android, the path returned by both methods should be used for storing the app-related data.
But there is a minor difference that for the files inside the path returned by getTemporaryDirectory , the system will automatically delete files in this directory as disk space is needed elsewhere on the device. The system will always delete older files first, based on the lastModifiedTime.
Storage permission won't be required for both the methods.
As per documentation :
getTemporaryDirectory :
Path to the temporary directory on the device that is not backed up and is suitable for storing caches of downloaded files.
Files in this directory may be cleared at any time. This does not
return a new temporary directory. Instead, the caller is responsible
for creating (and cleaning up) files or directories within this
directory. This directory is scoped to the calling application.
On iOS, this uses the NSCachesDirectory API.
On Android, this uses the getCacheDir API on the context.
getApplicationSupportDirectory :
Path to a directory where the application may place application
support files.
Use this for files you don’t want exposed to the user. Your app should
not use this directory for user data files.
On iOS, this uses the NSApplicationSupportDirectory API. If this
directory does not exist, it is created automatically.
On Android, this function uses the getFilesDir API on the context.

When to use assets in Flutter

What is the best way to use assets in Flutter , for example if i have a file for app configuration , should I store the file by getting the app directory using the path_provider plugin -without using assets- and store it ?, or should I add the file to my program folder -add the file to my assets- ?
the same question if I have a small Sqlite database.
and which of these methods is faster , and which is more secure ?
Assets are files that you add to your app during development. You can load them with rootBundle.load() or rootBundle.loadString() but you cannot modify or delete them.
In the app's directory you can store any files that your app downloads or generates from the internet while running. These files can then be opened, deleted, modified, etc. To access your app directory you need the package path_provider, which tells you the path to your app folder.
A sqlite database is normally stored in the app directory. An example package would be here sqflite.
For speed and security I can't make a difference. An app directory is designed so that only the app can access it. Assets are a part of the app, the application file can theoretically be unpacked by anyone. Therefore I would at least not store secret things in the assets.
Well, if by app configuration you mean the user's settings you can use Sqlite, SharedPreferences or Hive (Hive shows a benchmark that says that it is faster than SharedPreferences).
I believe that assets folder is used to store some common files for the app, like images, icons, fonts, etc. And I think that isn't recommended to store files with some kind of config file, mainly with critical info about the app configuration.

Write to a file in local storage flutter

I know about the path_provider package but it doesn't do what i want, or maybe I'm not just using it right. I read after so many trials and errors that the getApplicationDocumentsDirectory() returns a directory that is accessible only by the app itself, but what if i want to write to a phone's local document directory or so and be able to view the file in my file explorer later on?
If you want to save where the file explorer reaches, you must use the method getExternalStorageDirectory(). It only works in Android and you'll need READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions.
Actually, you're able to find the files saved using getApplicationDocumentsDirectory() and getTemporaryDirectory() as well, but you'd need root access.

Chrome App: How to enumerate files under a static folder?

How can I enumerate files under a static folder (such as images where all the static images used by my app are stored) while developing a chrome app?
What I have tried:
- looked at these links 1, 2. i am not trying to read from local or user storage. i want to read from static storage. static storage are the folders where an app stores its static contents - assets like images, css, js etc.
- looked at this link. but my app needs to be able to enumerate all files under a static folder without prompting the user for any action/permission.
i will give an example of why this is useful. imagine i want to release an app using which users can read my poems. i want a static folder where i save poems i write. then as i write new poems, these poems should then be made available automatically without having to do any code changes. further i was able to do this on android without any user prompt whatsoever so expect chrome platform should also support this - there cannot be any security excuses.
If you are looking to enumerate app's own files, it's possible via chrome.runtime.getPackageDirectoryEntry.
It returns a DirectoryEntry for the root of your app that you can navigate and iterate through.
See this question: How do I get a list of filenames in a subfolder of a Chrome extension?
You can't. The user will have to choose some parent folder with chrome.fileSystem.chooseEntry at some point. However, you can arrange things so this is done only once when the app is first installed, and then save the FileEntry using the retained-entry API.

Download / upload file using the Add-On SDK

I am currently trying to download a small binary file from the web, in order to upload that to another website, both using the API.
Previous versions seemed to have the "file" API module for such purposes, but I can't see anything similar as of the latest (1.14).
The file to be downloaded would be saved in some form of cache (browser cache, preferably), its path stored somewhere, to be then uploaded to another URL via POST.
How would I go about it, when the process should happen completely in the background?
I checked out the how to download a file page, but can't figure out where to download.
Is there a variable URI for the "Downloads" directory, and does a regular Add-On has write privileges in it?.
This is important, because the add-on must be able to function properly on various platforms.
You can use the pref, browser.download.lastDir, which should work for windows/mac as it will be saved in the OS format. However the pref may not always be set if the person has never downloaded anything before. In that case you'll have to build the directory yourself.
var dir = require("sdk/preferences/service").get('browser.download.lastDir');
To build the directory yourself you're going to have to go a little deeper. Check this article on MDN about File I/O which has examples. The DfltDwnld key should give you the directory you want.
Your add-on will have write permissions to everything Firefox has write permission to.