获取外部存储权限(file_provider 插件) [英] Getting permission to the external storage (file_provider plugin)

查看:29
本文介绍了获取外部存储权限(file_provider 插件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Flutter 获取对 android 设备上的外部存储的权限时遇到了一些问题.

I'm having some problems getting permissions to the external storage on android devices with flutter.

当我尝试在我的外部存储中创建一个目录时,我收到了这个错误(我只是为了这个例子更改了目录.在我自己的项目中,目录名称是不同的):

When I try to create a directory in my external storage I get this error (I've changed the directory just for this example. In my own project the directory name is different):

I/flutter (12727): ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
I/flutter (12727): The following FileSystemException was thrown while handling a gesture:
I/flutter (12727): Creation failed, path = '/storage/emulated/0/com.domain.bundle' (OS Error: Permission denied,
I/flutter (12727): errno = 13)
I/flutter (12727): 

我已经添加了
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
到清单文件并重建整个项目.我还能尝试什么?

I've already added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to the manifest file and rebuild the entire project. What else can I try?

也只是列出外部存储中的所有文件会导致相同的权限错误.

Also just listing all the files in the external storage gives the same permission error.

希望听到!

推荐答案

除了需要添加 WRITE_EXTERNAL_STORAGEREAD_EXTERNAL_STORAGE 到你的 android/app/src/main/AndroidManifest.xml

Beside needing to add WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE to your android/app/src/main/AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx.yyy">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
...
</manifest>

您还需要运行时请求权限,通过使用simple_permissions 包:

You also need Runtime Request Permission, by using simple_permissions package:

import 'package:simple_permissions/simple_permissions.dart';

PermissionStatus permissionResult = await SimplePermissions.requestPermission(Permission. WriteExternalStorage);
if (permissionResult == PermissionStatus.authorized){
  // code of read or write file in external storage (SD card)
}

注意:

  1. 第一次运行SimplePermissions.requestPermission时,应用会弹出一个窗口,您必须点击允许:
  1. when running SimplePermissions.requestPermission for the First Time, app will popup a window, you MUST click ALLOW:

给予许可.

  1. 如果您已经点击了DENY,则卸载调试应用程序并重新调试它以安装和修复此问题-> 触发弹出窗口并让您有机会点击ALLOW.
  1. If you already clicked DENY, then uninstall debug app and re-debug it to install and fix this -> trigger the popup window and give you chance to click ALLOW.

这篇关于获取外部存储权限(file_provider 插件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆