我已经从这里阅读了flutter saf包文档SAF,但我现在的问题是每次应用程序都要求提供权限,即使我已经提供了它。我想我在这里遗漏了一些关于如何使用 saf 包处理权限的内容,有人可以帮忙吗?
这是我到目前为止所做的。
Future<int> path() async {
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
if (androidInfo.version.sdkInt >= 29) {
Permission.storage.request();
await saf.getDirectoryPermission(isDynamic: true);
var isSync = await saf.sync();
if (isSync as bool) {
var _paths = await saf.getCachedFilesPath();
loadImage(_paths);
}
return 1;
} else {
return 0;
}
}
这是我如何使用它
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: path(),
builder: (BuildContext context, AsyncSnapshot<int> text) {
if (text.connectionState == ConnectionState.waiting) {
return const Scaffold(
body: Center(
child: Text("Loading....",style: TextStyle(color: Color(0xff090849),fontSize: 20),),
),
);
} else {
return Scaffold(
body: GridView.builder(
padding: const EdgeInsets.all(3.0),
itemCount: _paths.length,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 3,
childAspectRatio: 3/4,
),
itemBuilder:
(BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(5.0), // Adjust the value as needed
child: Material(
borderRadius: BorderRadius.circular(10),
elevation: 5,
clipBehavior: Clip.antiAliasWithSaveLayer,
color: Colors.teal.withOpacity(0.9),
child: InkWell(
splashColor: Colors.teal,
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ImageView(imagePath: _paths[index], key: UniqueKey()),
),
);
},
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(
height: 110,
child: Ink.image(
image: FileImage(File(_paths[index])),
fit: BoxFit.cover,
),
),
MaterialButton(
height: 10,
onPressed: () async {
saveFile(_paths[index]);
},child: const Text("Download",style: TextStyle(color: Colors.white),),)
],
),
),
)
);
},
),
);
}
});
}
对于像我这样面临类似问题的任何人,解决方案是从使用像“Android/yourpath”这样的目录开始,而不是像这样的“/storage/emulated/0/Android/yourpath”