diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index d1767ce4a..61f6cafc4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -22,8 +22,9 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va val mediaFetcher = MediaFetcher(context) override fun doInBackground(vararg params: Void): ArrayList { - if (!context.hasPermission(PERMISSION_WRITE_STORAGE)) + if (!context.hasPermission(PERMISSION_WRITE_STORAGE)) { return ArrayList() + } val config = context.config val groupedMedia = mediaFetcher.getMediaByDirectories(isPickVideo, isPickImage) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt index a329f3ced..6df8fc016 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -40,9 +40,42 @@ class MediaFetcher(val context: Context) { directories.remove(it) } + searchNewFiles(directories, showHidden) return directories } + // search for undiscovered media files in the folders, from which we already have some media files + private fun searchNewFiles(directories: Map>, 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() + + 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 { val projection = arrayOf(MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME,