如何在设备存储中创建文件夹以保存文件?
这是将文件下载到设备中的代码:
import 'package:flutter_downloader/flutter_downloader.dart';
onTap: () async { //ListTile attribute
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
final taskId = await FlutterDownloader.enqueue(
url: 'http://myapp/${attach[index]}',
savedDir: '/sdcard/myapp',
showNotification: true, // show download progress in status bar (for Android)
clickToOpenDownloadedFile: true, // click on notification to open downloaded file (for Android)
);
},
答案 0 :(得分:1)
从我看到的结果来看,您没有在任何地方使用appDocDir
和appDocPath
,因为您将文件保存在/sdcard/myapp
中。
请检查您是否在询问并授予存储许可,并且也无法像您一样将文件存储在sdcard中。可以使用预定义目录(例如,文档,图片等),也可以使用以storage/emulated/0
答案 1 :(得分:1)
您可以在启动应用程序时创建目录。
在第一个屏幕的initState()
方法中,执行逻辑。
例如。
createDir() async {
Directory baseDir = await getExternalStorageDirectory(); //only for Android
// Directory baseDir = await getApplicationDocumentsDirectory(); //works for both iOS and Android
String dirToBeCreated = "<your_dir_name>";
String finalDir = join(baseDir, dirToBeCreated);
var dir = Directory(finalDir);
bool dirExists = await dir.exists();
if(!dirExists){
dir.create(/*recursive=true*/); //pass recursive as true if directory is recursive
}
//Now you can use this directory for saving file, etc.
//In case you are using external storage, make sure you have storage permissions.
}
@override
initState(){
createDir(); //call your method here
super.initState();
}
您需要导入以下库:
import 'dart:io';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
答案 2 :(得分:0)
var A = {
hello : function() {
console.log('hello');
}
}
var B = {}
B.prototype = A;
var C = {}
C.prototype = B;
C.hello();
答案 3 :(得分:0)
这是在用户内部存储中创建文件夹的示例代码,希望它对您有帮助
import 'dart:io' as Io;
Future _downloadImage() async {
try {
// request runtime permission
final permissionHandler = PermissionHandler();
final status = await permissionHandler
.checkPermissionStatus(PermissionGroup.storage);
if (status != PermissionStatus.granted) {
final requestRes = await permissionHandler
.requestPermissions([PermissionGroup.storage]);
if (requestRes[PermissionGroup.storage] != PermissionStatus.granted) {
_showSnackBar('Permission denined. Go to setting to granted!');
return _done();
}
}
}
var testdir =
await new Io.Directory('/storage/emulated/0/MyApp').create(recursive: true);
final filePath =
path.join(testdir.path, Filename + '.png');
print(filePath);
final file = File(filePath);
if (file.existsSync()) {
file.deleteSync();
}
//save image to storage
var request = await HttpClient().getUrl(Uri.parse(imageUrl));
var response = await request.close();
final Uint8List bytes = await consolidateHttpClientResponseBytes(response);
final saveFileResult =
saveImage({'filePath': filePath, 'bytes': bytes});
_showSnackBar(
saveFileResult
? 'Image downloaded successfully'
: 'Failed to download image',
);
} on PlatformException catch (e) {
_showSnackBar(e.message);
} catch (e, s) {
_showSnackBar('An error occurred');
debugPrint('Download image: $e, $s');
}
return _done();
}
答案 4 :(得分:0)
首先您需要导入
1)导入'dart:io';
第二步,您需要在异步/等待功能中为指定路径创建目录
2)例如: 等待新目录('/storage/emulated/0/yourFolder').create(recursive:true);