Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

flutter - Getting permission to the external storage (file_provider plugin)

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): 

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.

Hope to hear!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

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)
}

Note:

  1. when running SimplePermissions.requestPermission for the First Time, app will popup a window, you MUST click ALLOW:

to give permission.

  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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...