I want to download images and save it to storage in a unique directory name using dio and path_provider.but i have an error:
I/flutter (15977): FileSystemException: Cannot open file,
path = '/data/user/0/com.manga.indonesia.manga.suka/app_flutter/sensei-wa-koi-o-oshie-rarenai-chapter-7-bahasa-indonesia/0.jpg'
How can i make this sensei-wa-koi-o-oshie-rarenai-chapter-7-bahasa-indonesia
directory?
My code :
downloadChapter(chapter) async {
Response res;
Dio dio = new Dio();
var dir = await getApplicationDocumentsDirectory();
try {
res = await dio
.get('http://0.0.0.0:8080/api/MangaSuka/kchapters/' + chapter);
var contentChapter = res.data['content'].where((a) {
return a != null;
}).toList();
for (var i = 0; i < contentChapter.length; i++) {
await dio.download(contentChapter[i], "${dir.path}/${chapter}/$i.jpg",
onProgress: (rec, total) {
print("Rec: $rec , Total: $total");
});
};
return print(contentChapter);
} catch (e) {
return print(e);
}
}
It is well explained in flutter documentation:
import 'dart:io';
void main() {
new Directory('sensei-wa-koi-o-oshie-rarenai-chapter-7-bahasa-indonesia').create()
// The created directory is returned as a Future.
.then((Directory directory) {
print(directory.path);
});
}
However, in many situations, like creating one directory, perhaps the async (default) version of Directory.create will not be time saving and could perhaps result in race condition. In that case, using the sync version of the command would be more appropriate:
new Directory('sensei-wa-koi-o-oshie-rarenai-chapter-7-bahasa-indonesia').createSync()
// ... following sequential code
Of note, many dart::io methods have *Sync versions.
import 'package:path_provider_ex/path_provider_ex.dart';
import 'dart:io';
import 'package:simple_permissions/simple_permissions.dart';
//packages
// simple_permissions: ^0.1.9
// path_provider_ex:
//Android mainfest
// <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
// <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
main(){
List<StorageInfo> _storageInfo = [];
@override
void initState() {
super.initState();
initPlatformState();
Timer(Duration(seconds: 2), () {
createFolder();
});
}
createFolder() async {
PermissionStatus permissionResult =
await SimplePermissions.requestPermission(
Permission.WriteExternalStorage);
if (permissionResult == PermissionStatus.authorized) {
// Directory _appFile = Directory(_storageInfo[0].rootDir + '/MyTestFOlder');
// _appFile.create();
new Directory(_storageInfo[0].rootDir + '/MyCreatedFolder').create()
// The created directory is returned as a Future.
.then((Directory directory) {
print(directory.path);
});
// File ourTempFile = File(_appFile.path);
// print(ourTempFile.path);
// ourTempFile.create();
// code of read or write file in external storage (SD card)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With