2015-08-22 145 views
-1

我使用RootTools库从hereRootTools.deleteFileOrDirectory始终返回false

授予root权限的应用程序后,我试图删除根使用内部存储的文件。

deleteStatus = RootTools.deleteFileOrDirectory(file.getAbsolutePath(), true); 

deleteStatus总是原来是错误的,该文件也不会被删除。

我在这里做错了什么?

UPDATE

我是新与ROOT使用。我在ROOT的应用程序中基本上有很少的要求。

1)我需要检查设备上是否有ROOT。 (RootTools.isRootAvailable())

2)I需要给一个ROOT权限提示用户授予超级用户权限(RootTools.isAccessGiven())

3)删除文件和文件夹(RootTools.deleteFileOrDirectory)

除删除方法外,一切都很完美。我如何使用libsuperuser来做到这一点?

+0

您可以将路径粘贴到文件吗? –

+0

是的,路径是/storage/emulated/0/logo_large_new.png –

回答

1

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) 。


这是一个冗长的答案。如果您还有其他问题,我会建议阅读任一图书馆项目的文档。

+0

感谢您的答案。实际上,如果我现在需要迁移图书馆,那么我需要做出很多改变。而libsuperuser对我来说似乎有点困难。如何修改RootTools中的删除方法,使其完美工作。 –

+0

你可以在GitHub上打开一个问题。我更新了我的答案。我的答案应该工作,因为你的文件不包含空格。 –

+0

其实,我正在测试内部存储。但用户也可以在外部SD卡上拥有文件,也可以使用空格的文件名。这种新方法能解决这两个问题吗? –