Issue
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:
Also the file I'm trying to open exists and seems fine - I can open it with other apps as well:
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"
Solution
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.
- I added
permission_handler:
topubspec.yaml
- 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);
}
}
Answered By - Cold_Class
Answer Checked By - Marilyn (JavaFixing Volunteer)