开发 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)相关推荐

  1. linux常见OS error code 值对照表

    当linux指令执行结束后: 返回值为 0,则表示程序执行成功. 返回值非 0,则表示程序执行出错,其提示所代表的意义可以参照以下error code 对照表: "OS error code ...

  2. 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 ...

  3. More than one file was found with OS independent path

    More than one file was found with OS independent path 解决方法 找到so库存放目录,找出相同的so文件,删掉其中一个,这个方法简单除暴,但是也是最 ...

  4. more than one file was found with os independent path'com/unionpay/blepayservice/'

    项目引用jar冲突或有相同的两份以上存在,系统发现了多个路径,造成系统不知道指向那个 报错:more than one file was found with os independent path ...

  5. git操作时出现error: cannot stat 'path/file': Permission denied

    git操作时出现error: cannot stat 'path/file': Permission denied 问题发生OS 场景 尝试 解决 问题发生OS Windows 10 场景 公司的小妹 ...

  6. R语言sys方法:sys.getpid函数获取R会话的进程ID、sys.glob函数和file.path函数匹配文件夹下的所有特定类型文件、sys.info函数获取系统和用户信息

    R语言sys方法:sys.getpid函数获取R会话的进程ID.sys.glob函数和file.path函数匹配文件夹下的所有特定类型文件.sys.info函数获取系统和用户信息 目录

  7. 文件系统中文件的最长(字符数)绝对路径 Longest Absolute File Path

    为什么80%的码农都做不了架构师?>>>    问题: Suppose we abstract our file system by a string in the followin ...

  8. 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 ...

  9. [Swift]LeetCode388. 文件的最长绝对路径 | Longest Absolute File Path

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

最新文章

  1. Go 知识点(06)— for range 切片并对切片进行 append 不会造成死循环
  2. 查看linux机器是32位还是64位的方法
  3. 基于 K8S 构建数据中心操作系统
  4. 收藏 | 深度学习损失函数大全
  5. 关于camera 构架设计的一点看法
  6. Grad-CAM (CNN可视化) Python示例
  7. zzuliOJ 2536: 绿绿学姐与AI 1
  8. 计算机三级网络操作题,计算机等级考试三级网络技术考试模拟试题
  9. informix linux安装步骤
  10. 虚拟机中CentOS 7 网络服务启动失败
  11. paip.软件版本完善计划VC423
  12. MIME,拓展名需要相应的软件打开
  13. H+ HPlus创建新的标签页/选项卡
  14. IIS6 配置ASP.NET网站伪静态
  15. 搞定签到码,一切皆可码!!!微信小程序篇
  16. 微信中的表情符号代码对照表
  17. 值得收藏,这6种制作竞赛动图的方法妙不可言
  18. 域名授权验证系统v1.0.6开源版本网站源码
  19. Linux 系统 CPU 占用率较高问题排查思路
  20. 金立(Gionee)金立M7 Power root 大金刚 GN5007 刷机TWRP 面具 XP框架 线刷包

热门文章

  1. 计算机二级c语言正确的做题顺序,雅思阅读:成绩提高的实践经验总结
  2. 统信UOS安装VM-Tools
  3. OS2.1.2:进程的状态与转换
  4. Cadence Allegro恢复软件菜单栏默认设置图文教程及视频演示
  5. Android View的绘制流程
  6. 最舒适最强大最全攻略玩法玩转最新win10——买不起mac学好win
  7. scale ambiguity issue 尺度模糊问题
  8. highway-env更改环境配置
  9. 平安银行薪资职级表_超全|阿里、腾讯、百度…这些大厂2020年薪资和职级一览...
  10. 计算机视觉的理论(北大 秦其明)