Android 如何创建包含空间的目录路径
每次我的应用程序尝试在SD卡上创建目录时,都会引发运行时异常。 错误是Android 如何创建包含空间的目录路径,android,file-io,runtimeexception,Android,File Io,Runtimeexception,每次我的应用程序尝试在SD卡上创建目录时,都会引发运行时异常。 错误是 java.lang.RuntimeException: Could not create library directory /storage/sdcard0/Seafile/logan676@163.com (cloud.seafile.com)/personal folder 所以我的问题是,是否允许创建包含空间的目录。 我实现了如下代码 if (path != null) { // Has recor
java.lang.RuntimeException: Could not create library directory /storage/sdcard0/Seafile/logan676@163.com (cloud.seafile.com)/personal folder
所以我的问题是,是否允许创建包含空间的目录。
我实现了如下代码
if (path != null) {
// Has record in database
repoDir = new File(path);
if (!repoDir.exists()) {
if (!repoDir.mkdirs()) {
throw new RuntimeException("Could not create library directory " + path);
}
}
return path;
}
我必须创建这样的目录,因为我在用户清除缓存时删除了它们
/**
* Deletes cache directory under a specific account<br>
* remember to clear cache from database after called this method
*
* @param dirPath
* @throws IOException
*/
public static void clearCache(String dirPath) throws IOException {
// clear all cached files inside of the directory, the directory itself included
File cacheDir = new File(dirPath);
FileUtils.deleteDirectory(cacheDir);
}
此clearCache方法将删除根目录及其子目录。
那么它是否与上述运行时异常有关。您的清单中是否有写入存储权限?是的,我有。像这样的语句是使用Environment.getXXXDirectory方法硬编码或生成的路径?顺便说一句,它在*nix操作系统上被认为是不好的形式,Android是其中一个在文件名中使用空格的操作系统,这使得正确操作文件的命令行变得更加困难。请尝试转义路径中的空格。如path=path.replace,\\;