2016-09-20 4 views
1

私たちの目的は、フォルダに対して読み書き可能な権限を設定することです。 [setReadable(bool)][1]および[setWritable(bool)][1]を使用します。それだ。Android - isDirectoryは常に新しいファイル(pathToDirectoryString)に対してfalseを返します

私たちはすでにファイルを書き、そこからファイルを読み込んでいますが、予防措置として明示的にこれらのアクセス許可を設定する必要があります。

コードは次のとおりです。何かが失われるのはmkdirs()からgetAbsolutePath()new FileisDirectory()ですか?我々はFOLDERPATHがディレクトリであるかどうかを確認する際、何らかの理由で、それは常にサービスでelswhere ...

//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)); 

...ログは明らかにディレクトリへのパスとして識別していても、falseを返すので...

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); 
      } 
     } 
    } 
+0

'mainFolder.mkdirs()でそれを置き換える代わりに!mainFolder.exists()

if (!File mainFolder.exists()) { 

!Fileをチェックしています;'。戻り値をチェックする必要があります。または、ディレクトリが存在するかどうか再度確認します。そして、もし戻らなければ。そうでない場合は、コードを続行しないでください。それを言うトースト()を表示してください。 – greenapps

+0

自分で作成したファイルやフォルダに対して、読み書き可能なアクセス許可を設定するのはどのような意味ですか? – greenapps

+0

'if(folderPath.isDirectory()){// <-------------これは常に偽です '。まず、そのパスが存在するかどうかを確認する必要があります()。存在しない場合は、mkdirs()の戻り値をチェックしなかったためです。 – greenapps

答えて

1

私はあなたがisDirectory()を呼び出したときにディレクトリがまだ存在していないので、それはfalseを返していると思います。

だから、それはdirectoty、CHEC場合だ場合は、チェックの前に存在している:

private void setPermissions(File folderPath) { 

    ... 

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

EDIT:

をあなたが作成するディレクトリの場合comprobationに誤りがあります。あなたは

if (!mainFolder.exists()) { 
+0

混乱を招いて申し訳ありません - 投稿する前に内容を十分に確認したと思いました。私はif(!mainFolder.exists()){をやっています。原作はおそらくコピー&ペーストエラーだった...謝罪。 – marienke

関連する問題