FileSystemException: Cannot open file while trying to write new file - flutter

I'm trying to write file with custom name but getting error but if i used constant name it works just fine
here is my code
Future downloadFile(String url, String name) async {
//creating path
final tempDir =
(await AndroidExternalStorage.getExternalStoragePublicDirectory(
DirType.downloadDirectory));
//filename genearted from
customname = generated;
final file = File('$tempDir/myfolder/$customname');
try {
final response = await Dio().get(url,
onReceiveProgress: (count, total) {
setState(() {
isDownloading = true;
progress = ((count / total) * 100);
if (progress == 100) {
isDownloaded = true;
}
});
},
options:
Options(responseType: ResponseType.bytes, receiveTimeout: 0));
var raf = file.openSync(mode: FileMode.write);
raf.writeByteSync(response.data);
await raf.close();
} catch (e) {
log(e.toString());
}
}
error log
[log] FileSystemException: Cannot open file, path = '/storage/emulated/0/Download/myfolder/dunkin.zip' (OS Error: Permission denied, errno = 13)

Related

Flutter: PlatformException (PlatformException(, cannot find the file, null, null)) on FlutterAudioRecorder2

class AudioRecorder {
static DateTime now = DateTime.now();
static String timestamp = now.toString();
var recorder = FlutterAudioRecorder2("./file_path",
audioFormat: AudioFormat.AAC);
// or var recorder = FlutterAudioRecorder2("file_path.mp3");
startRecording() async {
await recorder.initialized;
await recorder.start(); // <- error here
}
stopRecording() async {
var result = await recorder.stop();
}
}
I can't find a way to fix , mainly because I don't understand what does it means, why it says "cannot find the file" but the file needs to be created? (since it's a recording?)
Few points I noticed are
Please check if its initialised or not in the initstate
await recorder.initialized;
//after initialisation add this
var current = await recorder.current(channel: 0);
//you should get the current status as initialised if its done properly.
then on start recording use
await recorder.start()
Also make sure that the file path is right
Edit
This is the code to init the recorder
_init() async {
try {
bool hasPermission = await FlutterAudioRecorder2.hasPermissions ?? false;
if (hasPermission) {
String customPath = '/flutter_audio_recorder_';
io.Directory appDocDirectory;
// io.Directory appDocDirectory = await getApplicationDocumentsDirectory();
if (io.Platform.isIOS) {
appDocDirectory = await getApplicationDocumentsDirectory();
} else {
appDocDirectory = (await getExternalStorageDirectory())!;
}
// can add extension like ".mp4" ".wav" ".m4a" ".aac"
customPath = appDocDirectory.path + customPath + DateTime.now().millisecondsSinceEpoch.toString();
_recorder = FlutterAudioRecorder2(customPath, audioFormat: AudioFormat.AAC);
await _recorder!.initialized;
// after initialization
var current = await _recorder!.current(channel: 0);
print(current);
// should be "Initialized", if all working fine
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text("You must accept permissions")));
}
} catch (e) {
print(e);
}
}

FileSystemException: Cannot copy file ' (OS Error: Is a directory, errno = 21)

I am trying to copy a sqflite database file from this path (getDatabasesPath()) to external storage,but I got this exception:
FileSystemException: Cannot copy file to '/storage/emulated/0/databaseBackup', path = '/data/user/0/com.example.project/databases/roznamcha.db' (OS Error: Is a directory, errno = 21)
My code:
Future<bool> _requestPermission(Permission permission) async {
if (await permission.isGranted) {
return true;
} else {
var result = await permission.request();
if (result == PermissionStatus.granted) {
return true;
}
}
return false;
}
Future<bool> createDirectory() async {
Directory directory;
try {
if (Platform.isAndroid) {
if (await _requestPermission(Permission.storage)) {
directory = await getExternalStorageDirectory();
String newPath = "";
print(directory);
List<String> paths = directory.path.split("/");
for (int x = 1; x < paths.length; x++) {
String folder = paths[x];
if (folder != "Android") {
newPath += "/" + folder;
} else {
break;
}
}
newPath = newPath + "/databaseBackup";
directory = Directory(newPath);
} else {
return false;
}
} else {
if (await _requestPermission(Permission.photos)) {
directory = await getTemporaryDirectory();
} else {
return false;
}
}
if (!await directory.exists()) {
await directory.create(recursive: true);
}
if (await directory.exists()) {
final pathdb = await getDatabasesPath();
// join database name and database path
final path = join(pathdb, 'roznamcha.db');
print('path ra print ko: $path');
File f = File(path);
var cop = await f.copy(directory.path);
print(directory.path);
print('copy file $cop');
}
} catch (e) {
print(e);
}
return false;
}
I added permission_handler package and also permission configuration to AndroidManiFaes.xml but I still get this error.

Flutter download file in phone storage

I am not able to download file in the phone storage.. My code is..
Future<void> downloadFile() async {
Dio dio = Dio();
bool checkPermission1 =
await SimplePermissions.checkPermission(permission1);
print('checkPermission1');
print(checkPermission1);
if (checkPermission1 == false) {
await SimplePermissions.requestPermission(permission1);
checkPermission1 = await SimplePermissions.checkPermission(permission1);
}
if (checkPermission1 == true) {
String dirloc = "";
if (Platform.isAndroid) {
dirloc = "/sdcard/downloads/";
} else {
dirloc = (await getApplicationDocumentsDirectory()).path;
}
try {
FileUtils.mkdir([dirloc]);
await dio.download(url, dirloc + id + ".mp4",
onReceiveProgress: (receivedBytes, totalBytes) {
setState(() {
downloadingFlag = true;
progress =
((receivedBytes / totalBytes) * 100).toStringAsFixed(0) + "%";
});
});
} catch (e) {
print(e);
}
setState(() {
downloadingFlag = false;
downloadsFlag = false;
progress = "Download Completed.";
path = dirloc + id + ".mp4";
});
} else {
setState(() {
progress = "Permission Denied!";
});
}
}
I/flutter (23004): FileSystemException: Creation failed, path = '/sdcard/downloads' (OS Error: Permission denied, errno = 13)

Cannot save a file to the documents directory IOS Flutter

I tried to test the file download and saving to the documents directory. The code works on Android, but fails on IOS. Here is the code:
var dir = await getApplicationDocumentsDirectory();
var documentName = "";
documentName = "testname.pdf";
var storePath = "${dir.path}/$documentName";
var errorOccurred = false;
try {
await dio.download(url, storePath, onReceiveProgress: (rec, total) {
print("Rec: $rec , Total: $total");
_setState(() {
downloading = true;
progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";
});
});
} catch (e) {
print(e);
}
The error is the following:
Exception: PlatformException(file_not_found, File not found: '/../Devices/3D122270-E919-455D-AF3F-F048EC32CBB7/data/Containers/Data/Application/1262A294-59DC-47A5-B5A6-24FBAD9D53CA/Library/Caches/testname.pdf', null, null)
I am testing on simulator

readAsBytesSync is incomplete

Since I can't convert convert a file directly from url (e.g File(url)).
I am downloading the file and then use the temp file path.
I tried different files : images, pdfs and it's still incomplete.
Am I doing something wrong here?
Future<String> downloadFile() async {
print(imgUrl);
Dio dio = Dio();
try {
var dir = await getApplicationDocumentsDirectory();
await dio.download(imgUrl, "${dir.path}/${widget.name}.pdf",
onReceiveProgress: (rec, total) {});
path = "${dir.path}/${widget.name}.pdf";
setState(() {
downloading = false;
progressString = "Completed";
});
if (path != null) {
List<int> imageBytes = File(path).readAsBytesSync();
print("NEW BYTE : $imageBytes");
}
} catch (e) {
print(e);
}
return path;
}
Checkout this solution:-
https://gist.github.com/Nitingadhiya/3e029e2475eeffac311ecd76f273941f
Uint8List? _documentBytes;
getPdfBytes() async {
_documentBytes = await http.readBytes(Uri.parse('https://cdn.syncfusion.com/content/PDFViewer/flutter-succinctly.pdf'));
return _documentBytes;
}
Future<void> readPDf() async {
//Load the existing PDF document.
Uint8List documentInBytes = await getPdfBytes();
final PdfDocument document = PdfDocument(inputBytes: documentInBytes);
//Get the existing PDF page.
final PdfPage page = document.pages[0];
//Draw text in the PDF page.
page.graphics.drawString('Hello World!', PdfStandardFont(PdfFontFamily.helvetica, 12), brush: PdfSolidBrush(PdfColor(0, 0, 0)), bounds: const Rect.fromLTWH(0, 0, 150, 20));
//Save the document.
final List<int> bytes = await document.save(); //document.saveSync();
await saveAndLaunchFile(bytes, 'Invoice.pdf');
//Dispose the document.
document.dispose();
}
Future<void> saveAndLaunchFile(List<int> bytes, String fileName) async {
//Get the storage folder location using path_provider package.
String? path;
if (Platform.isAndroid || Platform.isIOS || Platform.isLinux || Platform.isWindows) {
final Directory directory = await path_provider.getApplicationSupportDirectory();
path = directory.path;
} else {
path = await PathProviderPlatform.instance.getApplicationSupportPath();
}
final File file = File(Platform.isWindows ? '$path\\$fileName' : '$path/$fileName');
await file.writeAsBytes(bytes, flush: true);
if (Platform.isAndroid || Platform.isIOS) {
//Launch the file (used open_file package)
// await open_file.OpenFile.open('$path/$fileName');
} else if (Platform.isWindows) {
await Process.run('start', <String>['$path\\$fileName'], runInShell: true);
} else if (Platform.isMacOS) {
await Process.run('open', <String>['$path/$fileName'], runInShell: true);
} else if (Platform.isLinux) {
await Process.run('xdg-open', <String>['$path/$fileName'], runInShell: true);
}
}