【发布时间】:2020-02-29 13:45:54
【问题描述】:
我试图在我的 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
cmets中的问题答案:
- “com.android.providers.media”不是我的应用程序的名称,我知道它是什么 - 但我要打开的图像在那里
- 我的 Android 版本是 8.0.0,我的手机也在运行 EMUI 8.0.0(我知道它到底是什么,一些华为的东西)
- 我可以使用手机自带的标准应用“图库”打开文件
【问题讨论】:
-
com.android.providers.media?那是你的应用吗? -
您使用的是 Android Q 吗?
-
I can open it with other apps as well:还有哪些应用程序?还是只有系统文件应用? -
您也可以自己尝试列出该目录中的文件。您在运行时要求用户确认清单中提到的权限?
-
所以你没有添加代码在运行时要求用户确认那些请求的权限?