RootTools.deleteFileOrDirectory 总是 returns 错误
RootTools.deleteFileOrDirectory always returns false
我正在使用来自 here
的 RootTools 库
给应用程序授予root权限后,我正在尝试使用Root删除内部存储中的文件。
deleteStatus = RootTools.deleteFileOrDirectory(file.getAbsolutePath(), true);
deleteStatus
总是被证明是假的,文件也没有被删除。
我做错了什么?
更新
我是 ROOT 用户的新手。我基本上对我的应用程序有 ROOT 的要求很少。
1) 我需要检查设备上是否有ROOT权限。 (RootTools.isRootAvailable())
2) 我需要给用户一个ROOT权限提示,让用户GRANT root权限(RootTools.isAccessGiven())
3) 删除文件和文件夹(RootTools.deleteFileOrDirectory)
除 delete 方法外,一切正常。我如何使用 libsuperuser 执行此操作?
RootTools 并不是最好的。就个人而言,我建议使用 libsuperuser.
您的文件未被删除的原因有很多。如果您查看 RootTools,它不会在路径周围添加引号。因此,如果您的文件包含空格,那么它不会被删除。
来自 RootTools:
Command command = new Command(0, false, "rm -r " + target);
Shell.startRootShell().add(command);
commandWait(Shell.startRootShell(), command);
应该是:
Command command = new Command(0, false, "rm -r \"" + target + "\"");
Shell.startRootShell().add(command);
commandWait(Shell.startRootShell(), command);
编辑:
无法在 shell 中读取 Environment.getExternalStorageDir()
返回的路径。在将命令发送到 shell.
之前,您需要更改路径
要解决此问题,您可以将以下静态工厂方法添加到您的项目中:
/**
* The external storage path is not readable by shell or root. This replaces {@link
* Environment#getExternalStorageDirectory()} with the environment variable "EXTERNAL_STORAGE".
*
* @param file
* The file to check.
* @return The original file (if it does not start with {@link
* Environment#getExternalStorageDirectory()}
* or a file with the correct path.
*/
@SuppressLint("SdCardPath")
public static File getFileForShell(File file) {
String externalStorage = Environment.getExternalStorageDirectory().getAbsolutePath();
if (!file.getAbsolutePath().startsWith(externalStorage)) {
return file;
}
String legacyStorage = System.getenv("EXTERNAL_STORAGE");
String path;
if (legacyStorage != null) {
path = file.getAbsolutePath().replaceFirst(externalStorage, legacyStorage);
} else {
path = file.getAbsolutePath().replaceFirst(externalStorage, "/sdcard");
}
return new File(path);
}
然后,当您调用 RootTools.deleteFileOrDirectory(String target, boolean remountAsRw);
时更改文件路径:
String path = getFileForShell(file).getAbsolutePath();
RootTools.deleteFileOrDirectory(path, true);
您不需要 root 访问权限即可删除内部存储上的文件。您需要在清单中声明的权限 android.permission.WRITE_EXTERNAL_STORAGE
。
libsuperuser
检查root权限是否可用并显示root权限提示,可以调用以下方法:
boolean isRooted = Shell.SU.available();
库 libsuperuser 并不打算做 RootTools 试图做的所有事情。如果您选择使用 libsuperuser,则需要将命令发送到 shell.
使用 libsuperuser 删除文件的示例:
void delete(File file) {
String command;
if (file.isDirectory()) {
command = "rm -r \"" + file.getAbsolutePath() + "\"";
} else {
command = "rm \"" + file.getAbsolutePath() + "\"";
}
Shell.SU.run(command);
}
请注意,这不会挂载文件系统 read/write 或检查 rm
是否在设备上可用(当您调用 deleteFileOrDirectory
时 RootTools 会执行此操作)。
这是一个冗长的答案。如果您还有其他问题,我建议您阅读任一图书馆项目的文档。
我正在使用来自 here
的 RootTools 库给应用程序授予root权限后,我正在尝试使用Root删除内部存储中的文件。
deleteStatus = RootTools.deleteFileOrDirectory(file.getAbsolutePath(), true);
deleteStatus
总是被证明是假的,文件也没有被删除。
我做错了什么?
更新
我是 ROOT 用户的新手。我基本上对我的应用程序有 ROOT 的要求很少。
1) 我需要检查设备上是否有ROOT权限。 (RootTools.isRootAvailable())
2) 我需要给用户一个ROOT权限提示,让用户GRANT root权限(RootTools.isAccessGiven())
3) 删除文件和文件夹(RootTools.deleteFileOrDirectory)
除 delete 方法外,一切正常。我如何使用 libsuperuser 执行此操作?
RootTools 并不是最好的。就个人而言,我建议使用 libsuperuser.
您的文件未被删除的原因有很多。如果您查看 RootTools,它不会在路径周围添加引号。因此,如果您的文件包含空格,那么它不会被删除。
来自 RootTools:
Command command = new Command(0, false, "rm -r " + target);
Shell.startRootShell().add(command);
commandWait(Shell.startRootShell(), command);
应该是:
Command command = new Command(0, false, "rm -r \"" + target + "\"");
Shell.startRootShell().add(command);
commandWait(Shell.startRootShell(), command);
编辑:
无法在 shell 中读取 Environment.getExternalStorageDir()
返回的路径。在将命令发送到 shell.
要解决此问题,您可以将以下静态工厂方法添加到您的项目中:
/**
* The external storage path is not readable by shell or root. This replaces {@link
* Environment#getExternalStorageDirectory()} with the environment variable "EXTERNAL_STORAGE".
*
* @param file
* The file to check.
* @return The original file (if it does not start with {@link
* Environment#getExternalStorageDirectory()}
* or a file with the correct path.
*/
@SuppressLint("SdCardPath")
public static File getFileForShell(File file) {
String externalStorage = Environment.getExternalStorageDirectory().getAbsolutePath();
if (!file.getAbsolutePath().startsWith(externalStorage)) {
return file;
}
String legacyStorage = System.getenv("EXTERNAL_STORAGE");
String path;
if (legacyStorage != null) {
path = file.getAbsolutePath().replaceFirst(externalStorage, legacyStorage);
} else {
path = file.getAbsolutePath().replaceFirst(externalStorage, "/sdcard");
}
return new File(path);
}
然后,当您调用 RootTools.deleteFileOrDirectory(String target, boolean remountAsRw);
时更改文件路径:
String path = getFileForShell(file).getAbsolutePath();
RootTools.deleteFileOrDirectory(path, true);
您不需要 root 访问权限即可删除内部存储上的文件。您需要在清单中声明的权限 android.permission.WRITE_EXTERNAL_STORAGE
。
libsuperuser
检查root权限是否可用并显示root权限提示,可以调用以下方法:
boolean isRooted = Shell.SU.available();
库 libsuperuser 并不打算做 RootTools 试图做的所有事情。如果您选择使用 libsuperuser,则需要将命令发送到 shell.
使用 libsuperuser 删除文件的示例:
void delete(File file) {
String command;
if (file.isDirectory()) {
command = "rm -r \"" + file.getAbsolutePath() + "\"";
} else {
command = "rm \"" + file.getAbsolutePath() + "\"";
}
Shell.SU.run(command);
}
请注意,这不会挂载文件系统 read/write 或检查 rm
是否在设备上可用(当您调用 deleteFileOrDirectory
时 RootTools 会执行此操作)。
这是一个冗长的答案。如果您还有其他问题,我建议您阅读任一图书馆项目的文档。