Android java.io.File.listFiles() not working prope

2019-03-01 19:12发布

问题:

I want to read file present on

Environment.getDataDirectory()

(data directory of android device) but I can't.

Can anybody help me on it.

Here is file code:

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:

That is because you do not have read access to that directory.