英文:
Flutter, Rooted Device. File Copy Permission denied
问题
我在一个Flutter应用中使用了root_access
和permission_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 '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);
}
}
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 '/storage/emulated/0/Documents/file.txt', path = '/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>
答案1
得分: 0
我在使用 **copy** 函数将文件从 **data** 文件夹复制到 **storage** 文件夹时找不到解决方案。但我找到了另一个解决方案。
所以我使用了 **root** 包而不是 **root_access**。
包链接 [root][1]。
[1]: https://pub.dev/packages/root
成功的代码片段如下。
import 'dart:async';
import 'dart:io';
import 'package:permission_handler/permission_handler.dart';
import 'package:root/root.dart';
Future<void> 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 = "/data/file.txt";
final rootFile = File(rootFileText);
// var copyTo = '/storage/emulated/0/Documents/file.txt';
var copyToFolder = '/storage/emulated/0/Documents/';
if (rootFile.existsSync()) {
String copycmd = "cp $rootFileText $copyToFolder";
if (isRooted && 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 'dart:async';
import 'dart:io';
import 'package:permission_handler/permission_handler.dart';
import 'package:root/root.dart';
Future<void> 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 = "/data/file.txt";
final rootFile = File(rootFileText);
// var copyTo = '/storage/emulated/0/Documents/file.txt';
var copyToFolder = '/storage/emulated/0/Documents/';
if (rootFile.existsSync()) {
String copycmd = "cp $rootFileText $copyToFolder";
if (isRooted && 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);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论