flutter Cannot open file, path = ‘.../test.jpg‘ (OS Error: Permission denied, errno = 13)
开发 flutter 遇到没权限问题,刚学不熟搞了半天,报错信息:
Cannot open file, path = '/storage/emulated/0/Download/test.jpg' (OS Error: Permission denied, errno = 13)
1. 在 AndroidManigfest.xml 添加需要的权限,AndroidManigfest.xml 的位置:
首先在 manifest 添加权限:
<!-- Permissions options for the `storage` group --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
然后在 <application> 中添加对 Android10 的访问权限:
android:requestLegacyExternalStorage="true"
如图:
2.利用 permission_handler 来申请权限
在 pubspec.yaml 添加:
permission_handler: ^5.0.1+1
包引入:
import 'package:permission_handler/permission_handler.dart';
代码中申请权限:
setPermission() async {if (await Permission.storage.request().isGranted) { //判断是否授权,没有授权会发起授权print("获得了授权");setState(() {setPhoto();});}else{print("没有获得授权");}}
3.下面是完整代码,判断是否有权限,没有权限申请权限然后获取图片来展示,有权限直接展示图片:
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:permission_handler/permission_handler.dart';void main() {runApp(MyApp());
}class MyApp extends StatelessWidget {const MyApp({Key? key}) : super(key: key);@overrideWidget build(BuildContext context) {return MaterialApp(title: 'Flutter Demo',theme: ThemeData(primarySwatch: Colors.blue,),home: const MyHomePage(title: 'Flutter Demo Home Page'),);}
}class MyHomePage extends StatefulWidget {const MyHomePage({Key? key, required this.title}) : super(key: key);final String title;@overrideState<MyHomePage> createState() => _MyHomePageState();
}class _MyHomePageState extends State<MyHomePage> {bool isPermission = false;@overrideWidget build(BuildContext context) {return Scaffold(appBar: AppBar(title: Text(widget.title),),body: setPhoto());}setPermission() async {if (await Permission.storage.request().isGranted) { //判断是否授权,没有授权会发起授权print("获得了授权");isPermission = true;setState(() {setPhoto();});}else{print("没有获得授权");isPermission = false;}}setPhoto() {if(isPermission){print("有授权");return Center(child:(Image.file(File('/storage/emulated/0/Download/test.jpg'),width: 100,height: 100)));}else{print("申请授权");setPermission();return null;}}
}
flutter Cannot open file, path = ‘.../test.jpg‘ (OS Error: Permission denied, errno = 13)相关推荐
- linux常见OS error code 值对照表
当linux指令执行结束后: 返回值为 0,则表示程序执行成功. 返回值非 0,则表示程序执行出错,其提示所代表的意义可以参照以下error code 对照表: "OS error code ...
- android异常 More than one file was found with OS independent path ‘META-INF/XXX‘
android异常 More than one file was found with OS independent path 'META-INF/XXX' 参考文章: (1)android异常 Mo ...
- More than one file was found with OS independent path
More than one file was found with OS independent path 解决方法 找到so库存放目录,找出相同的so文件,删掉其中一个,这个方法简单除暴,但是也是最 ...
- more than one file was found with os independent path'com/unionpay/blepayservice/'
项目引用jar冲突或有相同的两份以上存在,系统发现了多个路径,造成系统不知道指向那个 报错:more than one file was found with os independent path ...
- git操作时出现error: cannot stat 'path/file': Permission denied
git操作时出现error: cannot stat 'path/file': Permission denied 问题发生OS 场景 尝试 解决 问题发生OS Windows 10 场景 公司的小妹 ...
- R语言sys方法:sys.getpid函数获取R会话的进程ID、sys.glob函数和file.path函数匹配文件夹下的所有特定类型文件、sys.info函数获取系统和用户信息
R语言sys方法:sys.getpid函数获取R会话的进程ID.sys.glob函数和file.path函数匹配文件夹下的所有特定类型文件.sys.info函数获取系统和用户信息 目录
- 文件系统中文件的最长(字符数)绝对路径 Longest Absolute File Path
为什么80%的码农都做不了架构师?>>> 问题: Suppose we abstract our file system by a string in the followin ...
- python split(), os.path.split()和os.path.splitext()函数的区别
python split(), os.path.split()和os.path.splitext()函数的区别 文章目录: 1. split() 2. os.path.split() 3. os.pa ...
- [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
最新文章
- Go 知识点(06)— for range 切片并对切片进行 append 不会造成死循环
- 查看linux机器是32位还是64位的方法
- 基于 K8S 构建数据中心操作系统
- 收藏 | 深度学习损失函数大全
- 关于camera 构架设计的一点看法
- Grad-CAM (CNN可视化) Python示例
- zzuliOJ 2536: 绿绿学姐与AI 1
- 计算机三级网络操作题,计算机等级考试三级网络技术考试模拟试题
- informix linux安装步骤
- 虚拟机中CentOS 7 网络服务启动失败
- paip.软件版本完善计划VC423
- MIME,拓展名需要相应的软件打开
- H+ HPlus创建新的标签页/选项卡
- IIS6 配置ASP.NET网站伪静态
- 搞定签到码,一切皆可码!!!微信小程序篇
- 微信中的表情符号代码对照表
- 值得收藏,这6种制作竞赛动图的方法妙不可言
- 域名授权验证系统v1.0.6开源版本网站源码
- Linux 系统 CPU 占用率较高问题排查思路
- 金立(Gionee)金立M7 Power root 大金刚 GN5007 刷机TWRP 面具 XP框架 线刷包
热门文章
- 计算机二级c语言正确的做题顺序,雅思阅读:成绩提高的实践经验总结
- 统信UOS安装VM-Tools
- OS2.1.2:进程的状态与转换
- Cadence Allegro恢复软件菜单栏默认设置图文教程及视频演示
- Android View的绘制流程
- 最舒适最强大最全攻略玩法玩转最新win10——买不起mac学好win
- scale ambiguity issue 尺度模糊问题
- highway-env更改环境配置
- 平安银行薪资职级表_超全|阿里、腾讯、百度…这些大厂2020年薪资和职级一览...
- 计算机视觉的理论(北大 秦其明)