【发布时间】:2017-01-28 03:10:41
【问题描述】:
我们的目标是设置文件夹的可读可写权限。使用[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);
}
}
}
【问题讨论】:
-
mainFolder.mkdirs();。您应该检查返回值。或者再次检查目录是否存在。如果不返回。如果没有,请不要继续使用代码。显示一个 Toast() 这样说。 -
对自己创建的文件或文件夹设置可读或可写权限有什么意义?
-
if(folderPath.isDirectory()){ //<-------------THIS IS ALWAYS FALSE。您应该首先检查该路径是否存在()。如果它不存在那是因为你没有检查 mkdirs() 的返回值。 -
创建文件的if也有错误。我已经编辑了我的答案。检查一下
-
好收获@adalPaRi!
标签: android file file-permissions