Android对其他应用/data分区下文件的读取

来源:互联网 发布:淘宝企业店铺是啥意思 编辑:程序博客网 时间:2024/06/07 09:34

最近在做一个功能,需要判断一个第三方应用/data分区下的某个文件是否存在。未root的手机是不能访问到其他应用/data分区下的文件的,我现在使用的设备已经打开了root。
Java的File类可以对文件做一系列操作。开始的思路是通过File.listFiles()来获取目标应用的子文件列表,然后对指定的文件进行exists()判断。但是当进入到包名下的一个目录时,里面的文件夹用isDirectory()和isFile()判断,返回的都是false,同时用canRead()做判断,返回的也是false。判断是文件访问权限的问题,但是通过chmod 744之后,canRead()仍然返回false。看样子通过File对/data分区的操作仍然受系统安全性限制。
后来找到了另外一种方法来判断,通过执行shell命令ls -R ,获取指定应用目录下文件的列表,从而判断文件是否存在。
这是执行shell命令的方法

    public static ArrayList execCmdsforResult(String[] cmds) {        ArrayList<String> list = new ArrayList<String>();        try {            Process process = Runtime.getRuntime().exec("su");            OutputStream os = process.getOutputStream();            process.getErrorStream();            InputStream is = process.getInputStream();            int i = cmds.length;            for (int j = 0; j < i; j++) {                String str = cmds[j];                os.write((str + "\n").getBytes());            }            os.write("exit\n".getBytes());            os.flush();            os.close();            BufferedReader reader = new BufferedReader(new InputStreamReader(is));            while (true) {                String str = reader.readLine();                if (str == null)                    break;                list.add(str);            }            reader.close();            process.waitFor();            process.destroy();            return list;        } catch (Exception localException) {        }        return list;    }

调用时:

ArrayList<String> list = execCmdsforResult(new String[] {"cd /data/data/com.testapp", "ls -R})

便可以获取到目标目录下所有文件的列表。

阅读全文
0 0
原创粉丝点击