Reputation: 139
I have scoured the internet and am doing what everyone is saying to do but for some reason I can not save a file image or otherwise into the applications folder.
I have this method to get the applications directory...
Future<Directory> get _localAppPath async {
Directory? directory;
if (Platform.isAndroid) {
directory = await getApplicationDocumentsDirectory();
Utilities.logInfo('Local Android App Path is: ${directory.path}');
} else {
// if IOS Device
directory = await getTemporaryDirectory();
Utilities.logInfo('Local IOS App Path is: $directory');
}
return directory;
}
and I use it in my save method like so...
Future<void> saveProfileImageLocally(File _file) async {
try {
final appDirPath = await _localAppPath;
//Utilities.logWarning('New path is: ${appDirPath.path}');
final fileExt = extension(_file.path);
// Check is directory exists
Utilities.logWarning('FilePath: ${_file.path}');
File newFile = await _file.rename('${appDirPath.path}/images/profileImage$fileExt');
Utilities.logWarning('New path is: ${newFile.path}');
Storage.saveValue('profileImage', newFile.path);
} catch (e) {
Utilities.logError(e.toString());
}
}
I check permissions on every app launch so I know I have permissions but no matter what I keep getting this error that there is no such file or directory... I was trying to do use the copy function until I read another stackoverflow post.
FileSystemException: Cannot rename file to '/data/user/0/ca.company.example/app_flutter/images/profileImage.jpg', path = '/data/user/0/ca.company/example/cache/CAP370489784397780451.jpg' (OS Error: No such file or directory, errno = 2)
This should be a simple one line process from all the resources I keep reading online and tutorials and and and... So I'm pretty confused to what step I'm missing.
Any help would be much appreciated.
Upvotes: 0
Views: 1106
Reputation: 139
So after some extensive googling and trial and errors I finally made a few smaller methods to Get everything working.
First I create a directory which checkes if it already exists or not and tack on whatever folder name I'm trying to save in.
// Checked and working
Future<String> createFolderInAppDocDir(String folderName) async {
//Get this App Document Directory
final Directory _appDocDir = await getApplicationDocumentsDirectory();
//App Document Directory + folder name
final Directory _appDocDirFolder = Directory('${_appDocDir.path}/$folderName');
final Directory _appDocDirNewFolder = await _appDocDirFolder.create(recursive: true);
return _appDocDirNewFolder.path;
}
Then I made another method to implement the saving to the location based on what type of file im saving...
Future<File?> saveFileLocally(String type, File _file) async {
File tmp;
try {
// Create or get Folder location
final appDir = await createFolderInAppDocDir(type);
// Get File Extension
String fileExt = extension(_file.path);
// Copy file to new location
tmp = await _file.copy('$appDir/${Timestamp.now().millisecondsSinceEpoch}$fileExt');
// Delete cached file
final bool tmpExists = await tmp.exists();
if (tmpExists) _file.delete();
//Utilities.logWarning('New path is: ${tmp.path}');
return tmp;
} catch (e) {
Utilities.logError('Error: $e');
return null;
}
}
The Utilities.logError is just a debug statement color codes so I can find them easier. Now it works like a charm for anything I need to save.
Upvotes: 2
Reputation: 1732
You need to copy the file I think. just look at the below code
//File created.
File tmpFile = File(imageFile.path);
final appDir = await getApplicationDocumentsDirectory();
//filename - returns last part after the separator - path package.
final fileName = basename(imageFile.path);
//copy the file to the specified directory and return File instance.
tmpFile = await tmpFile.copy('${appDirPath.path}/images/profileImage$fileExt');
Upvotes: 0