Android - isDirectory总是为新文件返回false(pathToDirectoryString)

时间:2016-09-20 09:05:06

标签: android file file-permissions

我们的目标是为文件夹设置可读写权限。使用[setReadable(bool)][1][setWritable(bool)][1]。这就对了。

我们已经向他们写文件并从中读取文件,但作为预防措施,我们希望明确设置这些权限。

代码如下。从mkdirs()getAbsolutePath()new FileisDirectory()会丢失一些内容吗?因为出于某种原因,当我们检查folderPath是否是目录时,它总是返回false,即使日志清楚地将其标识为目录的路径......

//This is in the onCreate function of the service.
File mainFolder = new File(thisService.this.getExternalFilesDir(null), "mainFolder");

if (!mainFolder.exists()) {
    //The folder doesn't exist yet, so create it.
    mainFolder.mkdirs();

    //And then make the other folders we'll need.
    File confsFolder = new File(File mainFolder.getAbsoluteFile()+"/confs");
    confsFolder.mkdirs();

    File logsFolder =new File(File mainFolder.getAbsoluteFile()+"/logs");
    logsFolder.mkdirs();

    File packagesFolder = new File(File mainFolder.getAbsoluteFile()+"/packages");
    packagesFolder.mkdirs();

    }

//String variables holding the folder paths.
confsFolderPathString = mainFolder.getAbsolutePath() + "/confs/ ";
logsFolderPathString = mainFolder.getAbsolutePath() + "/logs/ ";
packagesFolderPathString = mainFolder.getAbsolutePath() + "/packages/ ";

setPermissions(new File(confsFolderPathString));
setPermissions(new File(logsFolderPathString));
setPermissions(new File(packagesFolderPathString));

......服务中的其他地方......

private void setPermissions(File folderPath) {
    Log.d(TAG, "setPermissions: ");
    //Credit to: http://stackoverflow.com/a/11482350/956975

    Log.d(TAG, "setPermissions: folderPath -> "+folderPath.getAbsoluteFile());
    //That log produces this strings:
    //setPermissions: folderPath -> /storage/emulated/0/Android/data/our.package.domain.and.project/files/mainFolder/confs/ 
    //setPermissions: folderPath -> /storage/emulated/0/Android/data/our.package.domain.and.project/files/mainFolder/logs/ 
    //setPermissions: folderPath -> /storage/emulated/0/Android/data/our.package.domain.and.project/files/mainFolder/packages/ 
    //Those are DIRECTORIES, right?

    //Get the list of files (which could include folders) in the folderPath.

    if(folderPath.isDirectory()){ //<-------------THIS IS ALWAYS FALSE

        File[] list = folderPath.listFiles();

        if(list != null && list.length > 0){
             for (File f : list) {    
                 if (f.isDirectory()) {

                    Log.d("setPermissions: ", "Dir: " + f.getAbsoluteFile());

                    //Set readable permissions
                    f.setReadable(true);                        
                    //Set writable permissions
                    f.setWritable(true);

                    //Go deeper into the directory
                    setPermissions(f, true, true);

                    } else {
                        Log.d("setPermissions: ", "File: " + f.getAbsoluteFile());

                        //Set readable permissions
                        f.setReadable(true)
                        //Set writable permissions
                        f.setWritable(true);

                    }
                }
            }else{
                try {
                    throw new Exception("setPermissions: Directory list is empty.");
                } catch (Exception e) {
                    Log.e(TAG, "setPermissions: Directory list is empty.", e);
                }
            }
        }else{
            try {
                throw new FileNotFoundException("setPermissions: "+folderPath.getAbsolutePath() + " is not a directory.");
            } catch (FileNotFoundException e) {
                Log.e(TAG, "setPermissions: "+folderPath.getAbsolutePath()+" is not a directory.", e);
            }
        }  
    }

1 个答案:

答案 0 :(得分:1)

我认为它返回false,因为当你调用isDirectory()时,目录还不存在。

所以,在检查之前是否存在直接问题,如果存在,则为chec:

private void setPermissions(File folderPath) {

    ...

    if(folderPath.exists() && folderPath.isDirectory()) {
      // ALL FUNCTIONALITY
    }
}

修改

你在创建目录的if comprobation中有一个错误。您正在检查!File而不是!mainFolder.exists()

if (!File mainFolder.exists()) {

将其替换为

if (!mainFolder.exists()) {