search for new media files in the folders, from which we already have files

This commit is contained in:
tibbi 2018-02-04 23:56:11 +01:00
parent b89c8e2321
commit 53d79ecea8
2 changed files with 35 additions and 1 deletions

View file

@ -22,8 +22,9 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
val mediaFetcher = MediaFetcher(context) val mediaFetcher = MediaFetcher(context)
override fun doInBackground(vararg params: Void): ArrayList<Directory> { override fun doInBackground(vararg params: Void): ArrayList<Directory> {
if (!context.hasPermission(PERMISSION_WRITE_STORAGE)) if (!context.hasPermission(PERMISSION_WRITE_STORAGE)) {
return ArrayList() return ArrayList()
}
val config = context.config val config = context.config
val groupedMedia = mediaFetcher.getMediaByDirectories(isPickVideo, isPickImage) val groupedMedia = mediaFetcher.getMediaByDirectories(isPickVideo, isPickImage)

View file

@ -40,9 +40,42 @@ class MediaFetcher(val context: Context) {
directories.remove(it) directories.remove(it)
} }
searchNewFiles(directories, showHidden)
return directories return directories
} }
// search for undiscovered media files in the folders, from which we already have some media files
private fun searchNewFiles(directories: Map<String, ArrayList<Medium>>, showHidden: Boolean) {
Thread {
// try not to delay the main media file loading
Thread.sleep(3000)
for ((path, dirMedia) in directories) {
if (path.contains("/.thumbnails/", true)) {
continue
}
// get the file parent this way, "path" is lowercased
val folder = File(dirMedia.first().path).parentFile
val files = folder.listFiles() ?: continue
val fileCnt = files.filter { it.isFile }.size
val newPaths = ArrayList<String>()
if (dirMedia.size != fileCnt) {
val dirPaths = dirMedia.map { it.path }
files.forEach {
val filePath = it.absolutePath
if ((showHidden || !it.name.startsWith(".")) && !dirPaths.contains(filePath)) {
if (it.exists() && it.length() > 0 && it.isImageVideoGif()) {
newPaths.add(it.absolutePath)
}
}
}
}
context.scanPaths(newPaths)
}
}.start()
}
fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean): ArrayList<Medium> { fun getFilesFrom(curPath: String, isPickImage: Boolean, isPickVideo: Boolean): ArrayList<Medium> {
val projection = arrayOf(MediaStore.Images.Media._ID, val projection = arrayOf(MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DISPLAY_NAME,