私たちの目的は、フォルダに対して読み書き可能な権限を設定することです。 [setReadable(bool)][1]
および[setWritable(bool)][1]
を使用します。それだ。Android - isDirectoryは常に新しいファイル(pathToDirectoryString)に対してfalseを返します
私たちはすでにファイルを書き、そこからファイルを読み込んでいますが、予防措置として明示的にこれらのアクセス許可を設定する必要があります。
コードは次のとおりです。何かが失われるのはmkdirs()
からgetAbsolutePath()
〜new File
〜isDirectory()
ですか?我々は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);
}
}
}
'mainFolder.mkdirs()でそれを置き換える代わりに
!mainFolder.exists()
の
!File
をチェックしています;'。戻り値をチェックする必要があります。または、ディレクトリが存在するかどうか再度確認します。そして、もし戻らなければ。そうでない場合は、コードを続行しないでください。それを言うトースト()を表示してください。 – greenapps自分で作成したファイルやフォルダに対して、読み書き可能なアクセス許可を設定するのはどのような意味ですか? – greenapps
'if(folderPath.isDirectory()){// <-------------これは常に偽です '。まず、そのパスが存在するかどうかを確認する必要があります()。存在しない場合は、mkdirs()の戻り値をチェックしなかったためです。 – greenapps