Difference between Application Documents Directory and Support Directory in path_provider? - flutter

I am new to path_provider. I am making a music app and want to download an mp3 file but I don't know which is the right way to download the file within the application.
I want to store the file with application storage so users can not access it directly.
so where i can store it?
final dir = await getApplicationDocumentsDirectory();
final dir2 = await getApplicationSupportDirectory();
print(dir.path);
print(dir2.path);
<---------- Output of path ----------->
/data/user/0/com.oraysa/app_flutter.
/data/user/0/com.oraysa/files
together with this one more question is how can I access downloaded files ( get info of all downloaded files). how can I find the same file so next time I don't have to download it again, instead just directly load it from storage?

Related

How to save file temporary?

I am using dio and path_provider to save pdf from url, I am going to show that pdf to user using open_file. But I have problem with saving path.
Where I can save file temporary, show that file, and after some time or when user will not look at the app delete file? File can be even deleted after 2 days. Time when pdf will be removed is not that important.
It can be even longer time, but sooner or later it must happen. This is in order to not have old pdf's stored on the phone in case of pdf's change on the web
Right now it is my path:
var tempDir = await getTemporaryDirectory();
String fullPath = "${tempDir.path}/test.pdf";
edit: there is also a second option, to check if something is in this place saved, if yes, then remove it and download again, but this place can not be shown to a users, I found "getApplicationDocumentsDirectory", this allows me to save pdf in place where users can not read a pdf.
After that reaserch my question will be, If I download my pdf in a way as you can see above it will stay in that place and will never be moved? Now I need to only creade a methode that will see if my pdf is there end will remove it? How to extract name of my pdf I will need that?
I guess you have selected the right path if you just needed to delete the file for the space on device because when you use the 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.
But if you want to check if the file changed or not you can send in the response of the file a flag that contains the last updated date and this value can be the file name prefix, so when ever you open the app you can check if the date has changed you will delete the old one manually and store the new one.
If you follow this solution it will be no need to use getTemporaryDirectory() you have to use the getApplicationDocumentsDirectory()

Download offline map at runtime in Flutter using Mapbox?

We have a flutter application where we want to use map functionality in offline mode.
Mapbox provides functionality in the flutter to use offline maps but .db file which contains offline data needs to be saved in the project at build time.
How to achieve the same at runtime?
also open to suggestions to use any other map service providers that works in both online and offline mode.
If you want to store a file on the device please read the official Flutter documentation about Reading and Writing Files
Essentially you need to add path_provider package to your project in order to retrieve the standard cache folder for every device and then simply save you file in it.
This is a my application similar behaviour code
//Get an available temporary dir on device using path_provider package
final Directory tempDir = await getTemporaryDirectory();
//Create a path with file name
final String tempPath = tempDir.path + '/' + 'yourFileName.db';
//Write file on device disk
final File file = File(tempPath);
await writeAsBytesSync(fileContent); //If it is a string use writeAsStringAsync
print('File written on disk');
Then using the file path you can simply read it from disk using readAsByteAsync method.
Remember that in the sample we are using getTemporaryDirectory() and as the documentation tell us
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.
I would recommend to check out this issue on the github page of the flutter-mapbox-gl repository:
https://github.com/tobrun/flutter-mapbox-gl/issues/88
See the comment of vinceKruger:
https://github.com/tobrun/flutter-mapbox-gl/issues/88#issuecomment-559380534
It is not an official way, but seems to work!

Flutter iOS/Andriod where to save data for easy download/transfer

I'm building an app that logs data and saves it on your phone.
Where is the correct place to save it, if i want to be able to easily transfer it off my phone later on?
According to the Flutter docs i would normally do something like this:
final directory = await getApplicationDocumentsDirectory();
return directory.path;
but this returns the path of
/data/user/0/com.myApp.myApp_app/app_flutter
Which i believe can only be accessed if you are root or the app that created the file.
So where and how do i save my file so that i can find it later in a file browser
**EDIT:**Here is what i ended up doing
create file like so File file = new File('/storage/emulated/0/Download/data.txt');
add <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> to your AndroidManifest.xml
Once the app is installed go into app info and toggle on permission for storage. I originally thought this would have been done automatically but guess not.
You were doing this right, but something was missing. I'm writing it out for you. It may help you. Let me know if that works.
What you can do is this :
String directory = (await getApplicationDocumentsDirectory()).path;
File file = new File('$directory/$your_file_name_in_your_local_device');
await file.writeAsBytes(dataInBytes);
print(file.path);
In order to get the data in bytes, please do this:
var dataInBytes = await request.bodyBytes;
Happy coding :)

With the Arcgis SDK, Is it possible to store a tile package (.tpk) or a compact cache on the sdcard in a Windows Store App ?

I can successfully load a .tkp package in a Windows Store App using the Arcgis SDK when the file is stored in the AppData Folder or the Instal Folder off the App.
Like this :
StorageFile sf = await ApplicationData.Current.LocalFolder.GetFileAsync("Test.tpk");
Layer bkgLayer = new ArcGISLocalTiledLayer(sf);
Map.Layers.Add(bkgLayer);
note : ApplicationData.Current.LocalFolder is the standard local folder :
C:\Users\username\AppData\Local\Packages\app_guid\LocalState
=> The base map is successfully displayed.
But, I have to store the data to the sd card or another folder like My document
So I use :
StorageFile sf= await Windows.Storage.StorageFile.GetFileFromPathAsync(#"C:\Users\<user>\Documents\Test.tpk");
Layer bkgLayer = new ArcGISLocalTiledLayer(sf);
Map.Layers.Add(bkgLayer);
=> The base map is not displayed
In the application manifest, I have access to the documents folder and to .tpk files. I can successfully read and write in this folder using a Stream.
I notice that in the SDK documentation we can read :
ArcGISLocalTiledLayer(StorageItem) : The storage file must be located in a location accessible by the app, ie only local data and app
storage. If the file resides outside these folders, it must be copied
to the local data store first.
So, is it a limitation of the Argis SDK or I made something wrong ?
Is there another way to store a base map outside the AppData folder ?
(The size of the base map is 40Go, and I have to share it between multiple user account)
Thanks a lot for any help :)

iOS - Allow user to download file from documents directory but not to upload file using iTunes

Here is a situation I have a data file in document directory which is being updated in the application every now and then. So I want to save it to my desktop using iTunes. But I don't want that the file should be uploaded back to my application. i.e. I want that user can download the file but can not upload any.
I was thinking to have the data file on some other location like Library and put a button on application settings saying "Prepare backup" that will copy that data file in Document directory, from where user can download it. If user uploads any thing it won't make any difference as my current file is in Library directory.
This is just a thought,
can anyone suggest me other way or the above way is good to go?
Edit: I just need that after the successful export user can view the data file (may be later) without support of the application.
You can't.
But what you can do is check (using an timer every 10 seconds) if a new file is added to the documents directory and then delete it programmatically.
But this ofcourse doesn't disable the replacement of files.