Flutter, Rooted Device. 文件复制权限被拒绝

huangapple go评论74阅读模式
英文:

Flutter, Rooted Device. File Copy Permission denied

问题

我在一个Flutter应用中使用了root_accesspermission_handler包来从数据文件夹复制文件到存储。尽管已经授予了所有所需的权限,但我收到以下错误:OS错误:权限被拒绝,错误号 = 13。这发生在一个已获取Root权限的Android设备上,运行Flutter版本3.7.11和Android SDK版本33.0.2。在AndroidManifest文件中,我添加了uses-permission。我能够从存储复制到存储而不会出现任何问题。

导致错误的代码片段如下,它试图将文件/data/file.txt复制到/storage/emulated/0/documents/file.txt

import 'dart:async';
import 'dart:io';
import 'package:permission_handler/permission_handler.dart';
import 'package:root_access/root_access.dart';

Future<void> copyrootfile() async {
    var status = await Permission.storage.status;
    var status2 = await Permission.accessMediaLocation.status;
    var status3 = await Permission.manageExternalStorage.status;
    if (!status.isGranted) {
        await Permission.storage.request();
    }
    if (!status2.isGranted) {
        await Permission.accessMediaLocation.request();
    }
    if (!status3.isGranted) {
        await Permission.manageExternalStorage.request();
    }
    await RootAccess.requestRootAccess;

    var rootFileText = "/data/file.txt";
    final rootFile = File(rootFileText);

    var copyTo = '/storage/emulated/0/documents/file.txt';
    if(rootFile.existsSync()){
        await rootFile.copy(copyTo);
    }
}

我相信我已经包含了所有必要的权限,所以不确定为什么复制操作会失败。有关如何解决此问题的建议吗?

更新
我收到了这个错误消息:

E/flutter (27371): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] 未处理的异常: FileSystemException: 无法将文件复制到 '/storage/emulated/0/Documents/file.txt',路径 = '/data/file.txt' (OS Error: Permission denied, errno = 13)
E/flutter (27371): #0      _checkForErrorResponse (dart:io/common.dart:42:9)
E/flutter (27371): #1      _File.copy.<anonymous closure> (dart:io/file_impl.dart:339:7)
E/flutter (27371): <asynchronous suspension>
E/flutter (27371): #2      _MyHomePageState.copyrootfile (package:rootcopy/main.dart:90:7)
E/flutter (27371): <asynchronous suspension>
E/flutter (27371): #3      _MyHomePageState.build.<anonymous closure> (package:rootcopy/main.dart:130:17)
E/flutter (27371): <asynchronous suspension>

请注意,文件路径大小写敏感,您的代码中路径大小写与错误消息中不匹配,可能是导致问题的原因之一。确保路径大小写一致。

英文:

I am using the root_access and permission_handler packages in a Flutter app to copy a file from the data folder to the storage. Although all required permissions are granted, I receive the following error: OS Error: Permission denied, errno = 13. This occurs on an Android device that is rooted and running Flutter version 3.7.11. and an Android SDK version 33.0.2. In the AndroidManifest File, I added uses-permission. I am able to copy from storage to storage without any issues.

The code snippet that causes the error is as follows, as it attempts to copy file /data/file.txt to /storage/emulated/0/documents/file.txt:

import &#39;dart:async&#39;;
import &#39;dart:io&#39;;
import &#39;package:permission_handler/permission_handler.dart&#39;;
import &#39;package:root_access/root_access.dart&#39;;

Future&lt;void&gt; copyrootfile() async {
    var status = await Permission.storage.status;
    var status2 = await Permission.accessMediaLocation.status;
    var status3 = await Permission.manageExternalStorage.status;
    if (!status.isGranted) {
      await Permission.storage.request();
    }
    if (!status2.isGranted) {
      await Permission.accessMediaLocation.request();
    }
    if (!status3.isGranted) {
      await Permission.manageExternalStorage.request();
    }
    await RootAccess.requestRootAccess;

    var rootFileText = &quot;/data/file.txt&quot;;
    final rootFile = File(rootFileText);

    var copyTo = &#39;/storage/emulated/0/documents/file.txt&#39;;
    if(rootFile.existsSync()){
        await rootFile.copy(copyTo);
    }
}

I believe I have included all necessary permissions, so I am not sure why the copy operation is failing. Any suggestions on how to fix this issue?

Update
I got this error message

E/flutter (27371): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FileSystemException: Cannot copy file to &#39;/storage/emulated/0/Documents/file.txt&#39;, path = &#39;/data/file.txt&#39; (OS Error: Permission denied, errno = 13)
E/flutter (27371): #0      _checkForErrorResponse (dart:io/common.dart:42:9)
E/flutter (27371): #1      _File.copy.&lt;anonymous closure&gt; (dart:io/file_impl.dart:339:7)
E/flutter (27371): &lt;asynchronous suspension&gt;
E/flutter (27371): #2      _MyHomePageState.copyrootfile (package:rootcopy/main.dart:90:7)
E/flutter (27371): &lt;asynchronous suspension&gt;
E/flutter (27371): #3      _MyHomePageState.build.&lt;anonymous closure&gt; (package:rootcopy/main.dart:130:17)
E/flutter (27371): &lt;asynchronous suspension&gt;

答案1

得分: 0

我在使用 **copy** 函数将文件从 **data** 文件夹复制到 **storage** 文件夹时找不到解决方案。但我找到了另一个解决方案。
所以我使用了 **root** 包而不是 **root_access**
包链接 [root][1]

  [1]: https://pub.dev/packages/root

成功的代码片段如下。

      import &#39;dart:async&#39;;
      import &#39;dart:io&#39;;
      import &#39;package:permission_handler/permission_handler.dart&#39;;
      import &#39;package:root/root.dart&#39;;

      Future&lt;void&gt; copyrootfile() async {
        bool isRooted = await Root.isRooted() ?? false;
        bool isRootAvailable = await Root.isRootAvailable() ?? false;
    
        var status = await Permission.storage.status;
        var status2 = await Permission.accessMediaLocation.status;
        var status3 = await Permission.manageExternalStorage.status;
        if (!status.isGranted) {
          await Permission.storage.request();
        }
        if (!status2.isGranted) {
          await Permission.accessMediaLocation.request();
        }
        if (!status3.isGranted) {
          await Permission.manageExternalStorage.request();
        }
    
        var rootFileText = &quot;/data/file.txt&quot;;
        final rootFile = File(rootFileText);
    
        // var copyTo = &#39;/storage/emulated/0/Documents/file.txt&#39;;
        var copyToFolder = &#39;/storage/emulated/0/Documents/&#39;;
    
        if (rootFile.existsSync()) {
          String copycmd = &quot;cp $rootFileText $copyToFolder&quot;;
          if (isRooted &amp;&amp; isRootAvailable) {
            // 检查文件夹是否存在。
            final checkFolderExist = await Directory(copyToFolder).exists();
            if (!checkFolderExist) {
              // 如果不存在则创建文件夹。
              await Directory(copyToFolder).create(recursive: true);
            }
            // 使用命令行复制。
            await Root.exec(cmd: copycmd);
          }
        }
      }
英文:

I can't find a solution when using copy function to copy file from data folder to storage. But I was able to find a solution instead.
So i used root package instead of root_access.
Package Link root.

The succesful code snippet is as follows.

  import &#39;dart:async&#39;;
import &#39;dart:io&#39;;
import &#39;package:permission_handler/permission_handler.dart&#39;;
import &#39;package:root/root.dart&#39;;
Future&lt;void&gt; copyrootfile() async {
bool isRooted = await Root.isRooted() ?? false;
bool isRootAvailable = await Root.isRootAvailable() ?? false;
var status = await Permission.storage.status;
var status2 = await Permission.accessMediaLocation.status;
var status3 = await Permission.manageExternalStorage.status;
if (!status.isGranted) {
await Permission.storage.request();
}
if (!status2.isGranted) {
await Permission.accessMediaLocation.request();
}
if (!status3.isGranted) {
await Permission.manageExternalStorage.request();
}
var rootFileText = &quot;/data/file.txt&quot;;
final rootFile = File(rootFileText);
// var copyTo = &#39;/storage/emulated/0/Documents/file.txt&#39;;
var copyToFolder = &#39;/storage/emulated/0/Documents/&#39;;
if (rootFile.existsSync()) {
String copycmd = &quot;cp $rootFileText $copyToFolder&quot;;
if (isRooted &amp;&amp; isRootAvailable) {
// Check folder Exist.
final checkFolderExist = await Directory(copyToFolder).exists();
if (!checkFolderExist) {
// Create folder if not Exist.
await Directory(copyToFolder).create(recursive: true);
}
// Copy using command line.
await Root.exec(cmd: copycmd);
}
}
}

huangapple
  • 本文由 发表于 2023年6月16日 13:35:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定