android - 如何摆脱 : "FileSystemException: Cannot open file, path = ' . 。 .' (OS Error: Permission denied, errno = 13)"?

标签 android flutter

我试图在我的 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文件,并且在运行该应用程序时,它表明它确实具有读取文件的权限:
Permissions
我试图打开的文件也存在并且看起来很好 - 我也可以用其他应用程序打开它:
File to open
这是我的代码:
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/

    相关文章:

    android - 如何打开 Gmail 查看邮件 Activity ?

    android - 通话记录器在 android 10 (Q) 中不起作用

    Flutter:OutlineButton的side属性好像没有效果

    ios - 使用 Xcode 构建 flutter 应用程序时命令代码符号失败并出现非零退出代码 (errSecInternalComponent)

    ios - 错误 : Runner. xcworkspace 不存在。实现谷歌地图时出错

    dart - 使用 Dart 操作 Google Sheet api

    android - google-services.json在创建我的应用程序的产品样式时导致问题,可能的解决方法?

    Android 动画 XML 问题

    android - 使用 "this"在 fragment 中调用上下文

    flutter 底溢出