Flutter: secret storages in phone - flutter

I have a encrypted file. I can decrypt this file. but when I decrpyt this file, I want to hide this file. Where can I hide this file. I try to hide cache. and I can put to cache. but file can be between 50MB to 150MB that is too much for cache. I need another way to hide. If you know a hiding place. Help please :)
this type is not solution. (.video.mp4)

You can save the encrypted files in app's dedicated storage so the user (or any other app) has no access to it and you should handle opening the file in your app. For more information look at this link.
import 'package:path_provider/path_provider.dart';
Future<void> saveEncryptedFile(File file) async {
final String dir = (await getApplicationDocumentsDirectory()).path;
final String path = '$dir/example.mp3';
final File file = File(path);
file.writeAsBytesSync(file);
}

Related

Difference between Application Documents Directory and Support Directory in path_provider?

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?

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()

Flutter - How to get gallery folder path?

I want to get gallery folder path.
I tried to use multiple libraries [path_provider, storage_path, image_save, multi_image_picker ...] to get the gallery folder path, but none of the libraries got the gallery folder path.
See, I don't find any great thing to save your file to your gallery, what I did found is to how to get your things done.
My solution will give you some things like
Save a file visible to the user
Check whether the file exists or not
Saving the file:
For saving the file we can rely on the package downloads_path_provider.
Look at this answer for using this package precisely
Flutter save file visible to the user - StackOverFlow would help you to know how he is using path_provider package to save his files.
Check whether the file exists or not
To do this, you can use this code inside your code, where you are trying to save the file, form the above answer.
Important: You will get the path of the file where you are storing the file, just pass the path in the below code
import 'dart:io' as io;
// for a file
bool exists = io.File(path).exists();
// for a directory
bool exists = io.Directory(path).exists();
if(exists) // do your operation
else // do your else operation
I am able to produce this much content for your problem. Any feedback are welcomed.

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 :)