How to get rid of: "FileSystemException: Cannot open file, path = '...' (OS Error: Permission denied, errno = 13)"?

Viewed 27573

I'm trying to display an image from my phone in my Flutter app and getting following error:

Another exception was thrown: FileSystemException: Cannot open file, path = '/storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1570277797774' (OS Error: Permission denied, errno = 13)

I don't understand the Permission denied part, because I added...

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

...to my android/app/src/mainAndroidManifest.xml file, and when running the app it shows that it really does have the permissions to read files:
Permissions
Also the file I'm trying to open exists and seems fine - I can open it with other apps as well: File to open Here's my Code:

import 'dart:io';

import 'package:flutter/material.dart';

void main() => runApp(FileOps());

class FileOps extends StatefulWidget {
  _FileOpsState createState() => _FileOpsState();
}

class _FileOpsState extends State<FileOps> {
  File localFile;
  String widgetTitle = 'Show Image';

  @override
  void initState() {
    super.initState();
    localfile.then((File _localFile) {
      setState(() {
        localFile = _localFile;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: widgetTitle,
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text(widgetTitle),
        ),
        body: Column(
          children: <Widget>[
            Image.file(localFile),
          ],
        ),
      ),
    );
  }

  Future<File> get localfile async {
    String imgPath =
        '/storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1570277797774';
    return File(imgPath);
  }
}

My pubspec.yaml:

name: template_app
description: A template Flutter app for testing and minimal examples

version: 1.0.0+1

environment:
  sdk: ">=2.1.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons:

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true

Answers to questions in comments:

  • "com.android.providers.media" is not the name of my app, idk what it is - but the image I want to open is there
  • My Android Version is 8.0.0, my phone is also running EMUI 8.0.0 (idk what it is exactly, some Huawei stuff)
  • I can open the file with the standard app that came with the phone called "Gallery"
5 Answers

Attention: My answer is a bit old now, I heard that a newer version of permission_handler works a bit differently now - so just google how it works nowadays, I guess.

I got it - <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> seemed to only define what permissions were needed, but not actually requesting them :D
So I manually requested the permissions and then everything worked:

I followed this tutorial.

  1. I added permission_handler: to pubspec.yaml
  2. Changed my get localfile get method to:
  Future<File> get localfile async {
    final PermissionHandler _permissionHandler = PermissionHandler();
    var result = await _permissionHandler.requestPermissions([PermissionGroup.storage]);
    if (result[PermissionGroup.storage] == PermissionStatus.granted) {
      // permission was granted
      String imgPath = '/storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1570277797774';
      return File(imgPath);
    }
  }

What Worked for me is

First in android/app/build.gradle

  • set your compileSdkVersion to 29 (current highest)

  • set your targetSdkVersion to 29 (current highest)

Secondly in android/app/src/main/AndroidManifest.xml

  • add android:requestLegacyExternalStorage="true to the tag

Then rebuild and see if it works for you

i got rid of this error by executing flutter clean && flutter pub get in the terminal

You can try restarting the code editor and the emulator if the external files changes may be slow message is displayed on your android studio. And then run flutter clean followed by pub get.

Doing this worked for me.

What i did when i faced the problem is i closed the editor and re-opened it after that i run the following commands sequentially before build in android studio.

1: flutter clean

2: flutter pub get

Related