diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt index 4c8e85e6f..58060754f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -4,7 +4,7 @@ import android.content.Context import android.database.Cursor import android.os.AsyncTask import android.provider.MediaStore -import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.commons.extensions.scanFiles import com.simplemobiletools.gallery.extensions.getLongValue import com.simplemobiletools.gallery.extensions.getStringValue import com.simplemobiletools.gallery.helpers.Config @@ -28,53 +28,51 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo val media = ArrayList() val invalidFiles = ArrayList() val showMedia = mConfig.showMedia - val uri = MediaStore.Files.getContentUri("external") - val where = "${MediaStore.Images.Media.DATA} LIKE ?" - val checkPath = if (showAll) "%" else "$mPath%" - val args = arrayOf(checkPath) - val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATE_MODIFIED, - MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.SIZE) - var cursor: Cursor? = null + for (i in 0..1) { + if (i == 0 && (isPickVideo || showMedia == VIDEOS)) + continue - try { - cursor = context.contentResolver.query(uri, columns, where, args, null) - if (cursor?.moveToFirst() == true) { - do { - val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) ?: continue - val file = File(curPath) - val size = cursor.getLongValue(MediaStore.Images.Media.SIZE) + var uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI + if (i == 1) { + if (isPickImage || showMedia == IMAGES) + continue - if (size == 0L) { - invalidFiles.add(file) - continue - } - - // exclude images of subdirectories - if (!showAll && file.parent != mPath) - continue - - var name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: "" - val isImage = file.isImageFast() || file.isGif() - val isVideo = file.isVideoFast() - - if (!isImage && !isVideo) - continue - - if (isVideo && (isPickImage || showMedia == IMAGES)) - continue - - if (isImage && (isPickVideo || showMedia == VIDEOS)) - continue - - if (name.isEmpty()) - name = curPath.getFilenameFromPath() - val dateModified = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED) - val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN) - media.add(Medium(name, curPath, isVideo, dateModified, dateTaken, size)) - } while (cursor.moveToNext()) + uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI + } + val where = "${MediaStore.Images.Media.DATA} LIKE ?" + val checkPath = if (showAll) "%" else "$mPath%" + val args = arrayOf(checkPath) + val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATE_MODIFIED, + MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.SIZE) + var cursor: Cursor? = null + + try { + cursor = context.contentResolver.query(uri, columns, where, args, null) + + if (cursor?.moveToFirst() == true) { + do { + val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) ?: continue + val file = File(curPath) + val size = cursor.getLongValue(MediaStore.Images.Media.SIZE) + + if (size == 0L) { + invalidFiles.add(file) + continue + } + + // exclude images of subdirectories + if (!showAll && file.parent != mPath) + continue + + val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: "" + val dateModified = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED) + val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN) + media.add(Medium(name, curPath, i == 1, dateModified, dateTaken, size)) + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() } - } finally { - cursor?.close() } context.scanFiles(invalidFiles) {} @@ -87,4 +85,4 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo super.onPostExecute(media) callback.invoke(media) } -} +} \ No newline at end of file