Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting permission to the external storage (file_provider plugin)

Tags:

flutter

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!

like image 559
Kevin Walter Avatar asked May 28 '18 08:05

Kevin Walter


People also ask

How do I get write permission for external storage?

To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest. xml file. Add these permissions just after the package name.

What does external storage permission mean?

When an app is granted storage permission, it can access the device storage at any time. This means it can upload personal files or even delete sensitive information from the device, so it's better to think twice before giving storage permission to untrusted apps, as it can be harmful.


2 Answers

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.
like image 134
Tree Avatar answered Sep 18 '22 20:09

Tree


In Android Q, this Error will be resolved by adding the Following Lines in AndroidManifest file:

 <application
      android:requestLegacyExternalStorage="true"
like image 37
Rajil TL Avatar answered Sep 21 '22 20:09

Rajil TL