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 :)
Related
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?
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()
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);
}
I'm a beginner at flutter and dart and had some issues with file handling using flutter.
I'm trying to store data locally in text files for my flutter application, and came across the standard tutorial on flutter's website which used path_provider and dart:io. It had instructions on getting the path and reading and writing to a file, however this didn't seem to work for files I added to the project. The only way to store to files was to first write using the inbuilt functions, then reading this newly created file. I can't add my own file and read it using flutter.
I also can't open the file created by flutter as I don't know where its stored. The path given by getApplicationDocumentsDirectory() returns the path /data/user/0/com.example.daily_challenges/app_flutter, and I don't know where to find this in my project.
In summary: How do I read a pre-existing file and how do I view the file created automatically by flutter.
Thanks :)
You can access the files from a physical device or simulator through Device File Explorer in Android Studio. Follow this guide
You won't be able to access getApplicationDocumentsDirectory().
If you are using android device, you can try to store it in getExternalStorageDirectory(). There's no equivalent in IOS though.
If you are running in a physical device. Open Device File Explorer in Android Studio and you can find the file under
data/data/your app package/app_flutter/fileName.txt
For example,
data/data/com.example.file_example/app_flutter/example.txt
And if you want to read the pre-existing file, you not need to anything specific, if you give the same file name, if not exist, it will create one, otherwise it will return the old one.
final File file = File(filePath);
file.writeAsStringSync('${text}', mode: FileMode.append);
For write, you consider using FileMode if you want to append text to the existing file. Or else by default overwrite will happen.
For read, you can consider this
final File file = File(filePath);
String text = await file.readAsString();
Just use Device File Explorer from Android Studio.
But the weird thing, path_provider gives you path like /data/user/0/your_app_id/..., but in fact all files are located in /data/data/your_app_id/..., as mentioned in previous answer.
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!