RootTools不是最大的。就我个人而言,我建议使用libsuperuser。
有很多原因为什么你的文件没有被删除。如果您查看RootTools,则不会在路径中添加引号。所以,如果你的文件包含空格,那么它不会被删除。
从RootTools:
它应该是:
Command command = new Command(0, false, "rm -r \"" + target + "\"");
Shell.startRootShell().add(command);
commandWait(Shell.startRootShell(), command);
编辑:
通过Environment.getExternalStorageDir()
返回的路径不能在shell读取。在将命令发送到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);
你不” t需要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);
}
请注意,这并不挂载文件系统的读/写或者检查是否rm
可在设备上(东西RootTools不会当你调用deleteFileOrDirectory
) 。
这是一个冗长的答案。如果您还有其他问题,我会建议阅读任一图书馆项目的文档。
您可以将路径粘贴到文件吗? –
是的,路径是/storage/emulated/0/logo_large_new.png –