pull/83/head
kunfei 5 years ago
parent 15cb3feee4
commit c5f86e0ea3
  1. 12
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 43
      app/src/main/java/io/legado/app/utils/FileUtils.kt

@ -36,18 +36,18 @@ object BookHelp {
return String.format("%05d-%s", bookChapter.index, MD5Utils.md5Encode(bookChapter.title))
}
private fun getBookCachePath(): String {
return "$downloadPath${File.separator}$cacheFolderName"
}
fun clearCache() {
if (downloadUri.isDocumentUri(App.INSTANCE)) {
DocumentFile.fromTreeUri(App.INSTANCE, downloadUri)
?.findFile(cacheFolderName)
?.delete()
} else {
FileUtils.deleteFile(getBookCachePath())
FileUtils.createFolderIfNotExist(getBookCachePath())
FileUtils.deleteFile(
FileUtils.getPath(
File(downloadPath),
subDirs = *arrayOf(cacheFolderName)
)
)
}
}

@ -13,28 +13,20 @@ import java.io.IOException
object FileUtils {
fun exists(file: File, fileName: String, vararg subDirs: String): Boolean {
val filePath =
file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName
return File(filePath).exists()
fun exists(root: File, fileName: String, vararg subDirs: String): Boolean {
return getFile(root, fileName, subDirs = *subDirs).exists()
}
fun createFileIfNotExist(file: File, fileName: String, vararg subDirs: String): File {
val filePath =
file.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName
fun createFileIfNotExist(root: File, fileName: String, vararg subDirs: String): File {
val filePath = getPath(root, fileName, *subDirs)
return createFileIfNotExist(filePath)
}
fun createFileIfNotExist(file: File, vararg subDirs: String): File {
val filePath = file.absolutePath + File.separator + subDirs.joinToString(File.separator)
fun createFileIfNotExist(root: File, vararg subDirs: String): File {
val filePath = root.absolutePath + File.separator + subDirs.joinToString(File.separator)
return createFolderIfNotExist(filePath)
}
fun getCachePath(): String {
return App.INSTANCE.externalCacheDir?.absolutePath
?: App.INSTANCE.cacheDir.absolutePath
}
fun createFolderIfNotExist(filePath: String): File {
val file = File(filePath)
//如果文件夹不存在,就创建它
@ -62,6 +54,24 @@ object FileUtils {
return file
}
fun getFile(file: File, fileName: String, vararg subDirs: String): File {
val filePath = getPath(file, fileName, *subDirs)
return File(filePath)
}
fun getFile(root: File, vararg subDirs: String): File {
val filePath = getPath(root, subDirs = *subDirs)
return File(filePath)
}
fun getPath(root: File, fileName: String? = null, vararg subDirs: String): String {
return if (fileName.isNullOrEmpty()) {
root.absolutePath + File.separator + subDirs.joinToString(File.separator)
} else {
root.absolutePath + File.separator + subDirs.joinToString(File.separator) + File.separator + fileName
}
}
//递归删除文件夹下的数据
@Synchronized
fun deleteFile(filePath: String) {
@ -79,6 +89,11 @@ object FileUtils {
file.delete()
}
fun getCachePath(): String {
return App.INSTANCE.externalCacheDir?.absolutePath
?: App.INSTANCE.cacheDir.absolutePath
}
fun getSdCardPath(): String {
@Suppress("DEPRECATION")
var sdCardDirectory = Environment.getExternalStorageDirectory().absolutePath

Loading…
Cancel
Save