【问题标题】:Can't View All Files In File Browser无法在文件浏览器中查看所有文件
【发布时间】:2012-01-25 23:12:22
【问题描述】:

我在创建的文件浏览器应用程序中遇到了这个奇怪的问题,我无法查看实际存在的每个文件/文件夹。似乎它们被过滤掉了。根资源管理器似乎可以很好地找到文件,但我的应用程序不能。有任何想法吗?是的,我的手机已经root了。

【问题讨论】:

  • 您的问题取决于您列出文件的方式。您使用的是File 类还是ls linux 命令?另外,您是否要列出/mnt/sdcard/ 或其他目录的内容,例如root (/) 或/system/
  • 感谢您的回复。我目前正在使用 File 类,但我不知道我可以使用 ls 命令。另外,我正在尝试从根 (/) 开始列出每个文件和文件夹的内容,无论是否隐藏。

标签: java android file root


【解决方案1】:

File 类不起作用的原因是您没有读取这些目录的权限。获得访问权限的唯一方法是读取/写入这些目录是使用 root 权限 - 要使用 root 权限,您必须运行基于 linux 的命令。

我不知道整个过程具体怎么做,但是你应该使用RootTools

通常要运行基于 linux 的命令,您会执行 Runtime.getRuntime.exec("command");,但使用 RootTools,整个过程被高度简化,并且也更容易获得命令的结果。

如果您愿意,我可以尝试进一步解释如何手动操作(执行 Runtime.getRuntime.exec("command"); 或者如果您浏览 Wiki 以获取 RootTools 并将其与您的应用程序集成,您可能会弄清楚这一点。

如果您还有其他问题,我很乐意尽我所能提供帮助。

编辑: 我看到了您的回答,因此您现在似乎对如何运行命令有了很好的了解。不过,RootTools 会让一切变得更轻松。

【讨论】:

  • 再次感谢!我一定会研究 RootTools。
【解决方案2】:

好的,我已经更多地研究了 shell 命令,并且这种方法有效,但这是最有效的方法吗?

public void ls(String directory) {  
                Process process = null;

                try {
                    process = Runtime.getRuntime().exec("ls " + directory);
                } catch (IOException e) {
                    e.printStackTrace();
                }    

                int read;
                BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                char[] buffer = new char[4096];
                StringBuilder output = new StringBuilder();

                try {
                    while ((read = reader.read(buffer)) > 0) {
                        output.append(buffer, 0, read);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }

                try {
                    process.waitFor();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }       

                Log.d("Files", output.toString());
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-09
    • 2012-04-10
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 2016-10-01
    • 2013-08-24
    相关资源
    最近更新 更多