我试图在我的 Flutter 应用程序中显示我手机中的图像并收到以下错误:
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)
我不明白
Permission denied
部分,因为我添加了... <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
...给我的
android/app/src/mainAndroidManifest.xml
文件,并且在运行该应用程序时,它表明它确实具有读取文件的权限:我试图打开的文件也存在并且看起来很好 - 我也可以用其他应用程序打开它:
这是我的代码:
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);
}
}
我的 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"
最佳答案
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.
我明白了 -
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
似乎只定义了需要哪些权限,但实际上并没有请求它们:D所以我手动请求了权限,然后一切正常:
我关注了this tutorial .
permission_handler:
至pubspec.yaml
get localfile
获取方法: 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);
}
}
关于android - 如何摆脱 : "FileSystemException: Cannot open file, path = ' . 。 .' (OS Error: Permission denied, errno = 13)"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60465796/