I just try to create folder follow this code
io.Directory appDocDirectory = await getApplicationDocumentsDirectory();
new io.Directory(appDocDirectory.path + '/' + 'dir').create(recursive: true)
// The created directory is returned as a Future.
.then((io.Directory directory) {
print('Path of New Dir: ' + directory.path);
});
and the result of this is
I/flutter (32540): Path of New Dir:
/data/user/0/[Package_name]/app_flutter/dir.
I am using real device and I don't have SdCard.
I can't find this folder. how I solved this?
You're looking at directory.path very likely before the future (from .then) has completed. You've got the right idea using await earlier. Just do that again.
Related
I have a flutter app that saves pdf files in a temporary directory then the user can press a button to delete these files, in Android everything works fine but in iOS I get this error:
Unhandled Exception: FileSystemException: Deletion failed, path =
'/var/mobile/Containers/Data/Application/75048CC8-24E7-4BA3-B748-5603D4F74F60/Library/Caches'
(OS Error: Operation not permitted, errno = 1)
The weird thing is also that if you refresh the app you can see that the files have been deleted so actually it works but gives error and blocks the app.
My code for deleting is this:
CupertinoDialogAction(
child: Text("Ok"),
onPressed: () async {
Directory dir = await getTemporaryDirectory();
dir.deleteSync(recursive: true);
dir.create();
Navigator.pop(context);
openFinishDialog();
}
),
Also this error shows only in physical devices and not on simulators.
I used the dart package path_provider 2.0.8
Okay I solved the problem by creating a directory inside the TemporaryDirectory and deleting that instead of the TemporaryDirectory, here's the code for creating:
var tempDir = await getTemporaryDirectory();
var tempDirPath = tempDir.path;
final myAppPath = '$tempDirPath/my_app';
final res = await Directory(myAppPath).create(recursive: true);
and here's the code for deleting:
Directory dir = await getTemporaryDirectory();
Directory myDir = Directory(dir.path + "/my_app");
myDir.deleteSync(recursive: true);
myDir.create();
I hope this is useful for someone else, if somebody has a better way to solve this issue please answer!
Creating a Directory inside the directory is the solution, you can also use getApplicationDocumentsDirectory() instead of getTemporaryDirectory() for android and ios.
I was working on a project that need some files to be downloaded into storage and later works offline . I am using Expo's Filesystem api to download the file and then saving the file in Expo FileSystem.documentDirectory
let directory = FileSystem.documentDirectory + 'media/';
await FileSystem.downloadAsync(imageurl,
directory + 'filename.jpg'
)
.then(({ uri }) => {
console.log(uri,'uri of image')
});
The problem is when i was updating app in itunesconnect the data is not persisting.
I guess you are trying to recover that file with absolute path after the update. Use a relative path instead.
iOS will update UUID of directory each time you change your build (ex. different builds on TestFlight or different version on AppStore).
ref: https://github.com/expo/expo/issues/4261#issuecomment-494072115
What you need to do is to store the filename into some persistent storage and combine that with FileSystem.documentDirectory to recover that file later, because your file is saved and the relative path works fine but the absolute path has changed.
Here is an example of copying an image into the document directory and recover that file later.
const to = FileSystem.documentDirectory + filename
await FileSystem.copyAsync({
from: uri, // uri to the image file
to
})
dispatch({ type: 'STORE_FILENAME', filename }) // Anything that persists. I use redux-persist
// ...after you updated the app to a different build
const filename = props.filename // recovered from the previous build anyhow
const uri = FileSystem.documentDirectory + filename
// maybe you want to render that image
return <Image uri={uri} />
Developing an app in electron, everything is working fine with file path in development mode. I created the "documents" folder, in which documents will be stored.
var dir = path.join(__dirname, 'documents');
fs.readdir(dir, function (err, files) {
if (err) {
console.log('Unable to scan directory: ' + err);
}
var files_with_dirs = files.map(function(name) {
return (dir + '/'+ name);
});
This code return all files in "documents" folder.
But in production mode when i pack my app, a folder with many files is created, the path becomes like this.
How to solve this problem?
For paths pointing to internal resources, I'd suggest using a relative path and building it on the fly
var p = upath.toUnix(upath.join(__dirname, "documents", "this-image.jpg));
Where __dirname is the path to the currently executing file.
I use the upath toUnix function because it normalizes the path to use forward slashes – which has worked better for me with cross--platform apps..
I'm working on a mobile app--I'm using flutter and it needs to view documents in my assets. I used the pdfviewer and it works perfectly fine. But with openfile, it does not.
I used the flutter package,
https://pub.dartlang.org/packages/flutter_pdf_viewer and it opens the pdf file but with this package, https://pub.dartlang.org/packages/open_file it won't open any files.
This is how i call them:
onPressed: () => FlutterPdfViewer.loadAsset("assets/files/sample.pdf"),
onPressed: () => OpenFile.open('assets/files/samplefile.docx'),
I don't know any more methods to open files in flutter. Am I using the open_file the wrong way? I hope you can help me figure this out.
Future<File> pdfAsset() async {
Directory tempDir = await getTemporaryDirectory();
File tempFile = File('${tempDir.path}/set_any_name.pdf');
ByteData bd = await rootBundle.load('assets/en/legal_notes.pdf');
await tempFile.writeAsBytes(bd.buffer.asUint8List(), flush: true);
return tempFile;
}
pdfAsset().then((file){OpenFile.open(file.path);});
Hi first copy the asset to temp file then open the file with OpenFile.
It works for me.
You need to update your pubspec.yaml with full path of the asset.
flutter:
assets:
- assets/files/samplefile.docx
I am creating a quiz app in the Flutter for which I have collected some questions in CSV file. I want to store the CSV file in firebase and display questions into the app by reading from the CSV file. But just to check if the reading file is as simple as it should be, I tried to read a dummy file in this way:
new File('file.txt').readAsString().then((String contents) {
print(contents);
});
from main.dart before returning the Widget.
But i get this error:
`FileSystemException: Cannot open file, path = 'file.txt' (OS Error: No such file or directory, errno = 2)`
even though I have made a dummy 'file.txt' file in the same directory as 'main.dart'.
I tried doing './file.txt' and even the absolute path from windows explorer but none seem to work.
How to fix this?
The path_provider package allows you to access the temp and appDir directory
https://pub.dartlang.org/packages/path_provider
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
You can use the join() method of https://pub.dartlang.org/packages/path to concatenate paths in a platform dependent way, or just use string concatenation like
String filePath = '${appDocDir.path}/file.txt';
new File(filePath).readAsString().then((String contents) {
print(contents);
});
You can use join to safely join files or folders:
Example:
import 'package:path/path.dart' as Path;
String fileName = Path.join('/storage/emulated/0/', 'MyFolder', 'file.txt');
Result:
fileName = '/storage/emulated/0/MyFolder/file.txt'