Android java.io.File.listFiles()在Environment.getDataDirectory()中无法正常工作

时间:2013-09-02 13:48:29

标签: android filesystems

我想阅读

上的文件
  

Environment.getDataDirectory()

(android设备的数据目录)但我不能。

任何人都可以帮助我。

这是文件代码:

File aFile = new File(Environment.getDataDirectory(), "/");
File file = new File(Environment.getExternalStorageDirectory(), "/filelist.txt");

public void Process(File aFile) {
    spc_count++;

    try {
        fos = new FileOutputStream(file,true);
        for (int i = 0; i < spc_count; i++)
            spcs += " ";
        if(aFile.isFile()){

            fos.write(aFile.getPath().getBytes());
            fos.write('|');
        }
        else if (aFile.isDirectory()) {

            File[] listOfFiles = aFile.listFiles();
            if(listOfFiles!=null) {
                for (int j = 0; j < listOfFiles.length; j++)
                    Process(listOfFiles[j]);
            } else {
                //System.out.println(spcs + " [ACCESS DENIED]");
            }
        }
        spc_count--;
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        // handle exception
    } catch (IOException e) {
        // handle exception
    }
}

1 个答案:

答案 0 :(得分:2)

这是因为您没有该目录的读取权限。