我想下载图像并使用dio和path_provider将其保存到唯一目录名称的存储中。但是我遇到了错误:
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'
如何创建此sensei-wa-koi-o-oshie-rarenai-chapter-7-bahasa-indonesia
目录?
我的代码:
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);
}
}
答案 0 :(得分:2)
在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);
});
}
答案 1 :(得分:1)
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)
}
}
}