我们的目标是设置文件夹的可读可写权限。使用 [setReadable(bool)][1]
和 [setWritable(bool)][1]
。这就对了。
我们已经向它们写入文件并从中读取文件,但作为预防措施,我们希望明确设置这些权限。
代码如下。从 mkdirs()
到 getAbsolutePath()
到 new File
到 isDirectory()
有什么东西丢失了吗?因为出于某种原因,当我们检查 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);
}
}
}
最佳答案
我认为它返回 false,因为当您调用 isDirectory()
时该目录尚不存在。
因此,在检查它是否是目录之前,检查是否存在:
private void setPermissions(File folderPath) {
...
if(folderPath.exists() && folderPath.isDirectory()) {
// ALL FUNCTIONALITY
}
}
编辑:
您在创建目录的 if comprobation 中有一个错误。您正在检查 !File
而不是 !mainFolder.exists()
if (!File mainFolder.exists()) {
替换为
if (!mainFolder.exists()) {
关于Android - isDirectory 始终为新文件 (pathToDirectoryString) 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39589928/