mirror of
https://github.com/FossifyOrg/Gallery.git
synced 2025-01-18 06:17:59 +01:00
rework the way of obtaining images and videos
This commit is contained in:
parent
23d85f6ff9
commit
c09e472a14
2 changed files with 82 additions and 101 deletions
|
@ -4,12 +4,15 @@ import android.content.Context
|
||||||
import android.database.Cursor
|
import android.database.Cursor
|
||||||
import android.os.AsyncTask
|
import android.os.AsyncTask
|
||||||
import android.provider.MediaStore
|
import android.provider.MediaStore
|
||||||
import com.simplemobiletools.commons.extensions.scanFiles
|
import com.simplemobiletools.commons.extensions.isGif
|
||||||
|
import com.simplemobiletools.commons.extensions.isImageFast
|
||||||
|
import com.simplemobiletools.commons.extensions.isVideoFast
|
||||||
import com.simplemobiletools.gallery.R
|
import com.simplemobiletools.gallery.R
|
||||||
import com.simplemobiletools.gallery.extensions.getHumanizedFilename
|
import com.simplemobiletools.gallery.extensions.getHumanizedFilename
|
||||||
import com.simplemobiletools.gallery.extensions.getLongValue
|
|
||||||
import com.simplemobiletools.gallery.extensions.getStringValue
|
import com.simplemobiletools.gallery.extensions.getStringValue
|
||||||
import com.simplemobiletools.gallery.helpers.*
|
import com.simplemobiletools.gallery.helpers.Config
|
||||||
|
import com.simplemobiletools.gallery.helpers.IMAGES
|
||||||
|
import com.simplemobiletools.gallery.helpers.VIDEOS
|
||||||
import com.simplemobiletools.gallery.models.Directory
|
import com.simplemobiletools.gallery.models.Directory
|
||||||
import com.simplemobiletools.gallery.models.Medium
|
import com.simplemobiletools.gallery.models.Medium
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
@ -24,50 +27,70 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
|
||||||
mConfig = Config.newInstance(context)
|
mConfig = Config.newInstance(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
|
private fun getWhereCondition(): String {
|
||||||
val media = ArrayList<Medium>()
|
|
||||||
val directories = LinkedHashMap<String, Directory>()
|
|
||||||
val invalidFiles = ArrayList<File>()
|
|
||||||
val showMedia = mConfig.showMedia
|
val showMedia = mConfig.showMedia
|
||||||
for (i in 0..1) {
|
return if ((isPickImage || showMedia == IMAGES) || (isPickVideo || showMedia == VIDEOS)) {
|
||||||
if (i == 0 && (isPickVideo || showMedia == VIDEOS))
|
"${MediaStore.Files.FileColumns.MEDIA_TYPE} = ?)"
|
||||||
continue
|
} else {
|
||||||
|
"${MediaStore.Files.FileColumns.MEDIA_TYPE} = ? OR ${MediaStore.Files.FileColumns.MEDIA_TYPE} = ?)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
|
private fun getArgs(): Array<String> {
|
||||||
if (i == 1) {
|
val showMedia = mConfig.showMedia
|
||||||
if (isPickImage || showMedia == IMAGES)
|
return if (isPickImage || showMedia == IMAGES) {
|
||||||
continue
|
arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString())
|
||||||
|
} else if (isPickVideo || showMedia == VIDEOS) {
|
||||||
|
arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO.toString())
|
||||||
|
} else {
|
||||||
|
arrayOf(MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE.toString(), MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
|
override fun doInBackground(vararg params: Void): ArrayList<Directory> {
|
||||||
}
|
val directories = LinkedHashMap<String, Directory>()
|
||||||
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATE_MODIFIED,
|
val media = ArrayList<Medium>()
|
||||||
MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.SIZE)
|
val showMedia = mConfig.showMedia
|
||||||
val order = getSortOrder()
|
val uri = MediaStore.Files.getContentUri("external")
|
||||||
var cursor: Cursor? = null
|
val where = getWhereCondition() + " GROUP BY ( ${MediaStore.Files.FileColumns.PARENT} "
|
||||||
|
val args = getArgs()
|
||||||
|
val columns = arrayOf(MediaStore.Files.FileColumns.PARENT, MediaStore.Images.Media.DATA)
|
||||||
|
var cursor: Cursor? = null
|
||||||
|
|
||||||
try {
|
try {
|
||||||
cursor = context.contentResolver.query(uri, columns, null, null, order)
|
cursor = context.contentResolver.query(uri, columns, where, args, null)
|
||||||
|
if (cursor?.moveToFirst() == true) {
|
||||||
|
do {
|
||||||
|
val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA)
|
||||||
|
val dirPath = File(curPath).parent
|
||||||
|
val dir = File(dirPath).listFiles() ?: continue
|
||||||
|
|
||||||
if (cursor?.moveToFirst() == true) {
|
for (file in dir) {
|
||||||
do {
|
val isImage = file.isImageFast() || file.isGif()
|
||||||
val fullPath = cursor.getStringValue(MediaStore.Images.Media.DATA) ?: continue
|
val isVideo = file.isVideoFast()
|
||||||
val file = File(fullPath)
|
|
||||||
val size = cursor.getLongValue(MediaStore.Images.Media.SIZE)
|
|
||||||
|
|
||||||
if (size == 0L) {
|
if (!isImage && !isVideo)
|
||||||
invalidFiles.add(file)
|
|
||||||
continue
|
continue
|
||||||
}
|
|
||||||
|
|
||||||
val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: ""
|
if (isVideo && (isPickImage || showMedia == IMAGES))
|
||||||
val dateModified = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED)
|
continue
|
||||||
val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
|
|
||||||
media.add(Medium(name, fullPath, i == 1, dateModified, dateTaken, size))
|
if (isImage && (isPickVideo || showMedia == VIDEOS))
|
||||||
} while (cursor.moveToNext())
|
continue
|
||||||
}
|
|
||||||
} finally {
|
val size = file.length()
|
||||||
cursor?.close()
|
if (size == 0L)
|
||||||
|
continue
|
||||||
|
|
||||||
|
val name = file.name
|
||||||
|
val path = file.absolutePath
|
||||||
|
val dateModified = file.lastModified()
|
||||||
|
media.add(Medium(name, path, isVideo, dateModified, dateModified, size))
|
||||||
|
}
|
||||||
|
} while (cursor.moveToNext())
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
cursor?.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
Medium.sorting = mConfig.fileSorting
|
Medium.sorting = mConfig.fileSorting
|
||||||
|
@ -90,7 +113,6 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
context.scanFiles(invalidFiles) {}
|
|
||||||
val dirs = ArrayList(directories.values.filter { File(it.path).exists() })
|
val dirs = ArrayList(directories.values.filter { File(it.path).exists() })
|
||||||
|
|
||||||
filterDirectories(dirs)
|
filterDirectories(dirs)
|
||||||
|
@ -115,20 +137,6 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
|
||||||
callback.invoke(dirs)
|
callback.invoke(dirs)
|
||||||
}
|
}
|
||||||
|
|
||||||
// sort the files at querying too, just to get the correct thumbnail
|
|
||||||
private fun getSortOrder(): String {
|
|
||||||
val sorting = mConfig.directorySorting
|
|
||||||
var sortBy = MediaStore.Images.Media.DATE_MODIFIED
|
|
||||||
if (sorting and SORT_BY_NAME != 0) {
|
|
||||||
sortBy = MediaStore.Images.Media.DATA
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sorting and SORT_DESCENDING != 0) {
|
|
||||||
sortBy += " DESC"
|
|
||||||
}
|
|
||||||
return sortBy
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun filterDirectories(dirs: MutableList<Directory>) {
|
private fun filterDirectories(dirs: MutableList<Directory>) {
|
||||||
if (!mConfig.showHiddenFolders) {
|
if (!mConfig.showHiddenFolders) {
|
||||||
removeHiddenFolders(dirs)
|
removeHiddenFolders(dirs)
|
||||||
|
|
|
@ -1,12 +1,10 @@
|
||||||
package com.simplemobiletools.gallery.asynctasks
|
package com.simplemobiletools.gallery.asynctasks
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.database.Cursor
|
|
||||||
import android.os.AsyncTask
|
import android.os.AsyncTask
|
||||||
import android.provider.MediaStore
|
import com.simplemobiletools.commons.extensions.isGif
|
||||||
import com.simplemobiletools.commons.extensions.*
|
import com.simplemobiletools.commons.extensions.isImageFast
|
||||||
import com.simplemobiletools.gallery.extensions.getLongValue
|
import com.simplemobiletools.commons.extensions.isVideoFast
|
||||||
import com.simplemobiletools.gallery.extensions.getStringValue
|
|
||||||
import com.simplemobiletools.gallery.helpers.Config
|
import com.simplemobiletools.gallery.helpers.Config
|
||||||
import com.simplemobiletools.gallery.helpers.IMAGES
|
import com.simplemobiletools.gallery.helpers.IMAGES
|
||||||
import com.simplemobiletools.gallery.helpers.VIDEOS
|
import com.simplemobiletools.gallery.helpers.VIDEOS
|
||||||
|
@ -26,58 +24,33 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
|
||||||
|
|
||||||
override fun doInBackground(vararg params: Void): ArrayList<Medium> {
|
override fun doInBackground(vararg params: Void): ArrayList<Medium> {
|
||||||
val media = ArrayList<Medium>()
|
val media = ArrayList<Medium>()
|
||||||
val invalidFiles = ArrayList<File>()
|
|
||||||
val showMedia = mConfig.showMedia
|
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
|
|
||||||
|
|
||||||
try {
|
val dir = File(mPath)
|
||||||
cursor = context.contentResolver.query(uri, columns, where, args, null)
|
val files = dir.listFiles() ?: return media
|
||||||
if (cursor?.moveToFirst() == true) {
|
for (file in files) {
|
||||||
do {
|
val isImage = file.isImageFast() || file.isGif()
|
||||||
val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) ?: continue
|
val isVideo = file.isVideoFast()
|
||||||
val file = File(curPath)
|
|
||||||
val size = cursor.getLongValue(MediaStore.Images.Media.SIZE)
|
|
||||||
|
|
||||||
if (size == 0L) {
|
if (!isImage && !isVideo)
|
||||||
invalidFiles.add(file)
|
continue
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// exclude images of subdirectories
|
if (isVideo && (isPickImage || showMedia == IMAGES))
|
||||||
if (!showAll && file.parent != mPath)
|
continue
|
||||||
continue
|
|
||||||
|
|
||||||
var name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: ""
|
if (isImage && (isPickVideo || showMedia == VIDEOS))
|
||||||
val isImage = file.isImageFast() || file.isGif()
|
continue
|
||||||
val isVideo = file.isVideoFast()
|
|
||||||
|
|
||||||
if (!isImage && !isVideo)
|
val size = file.length()
|
||||||
continue
|
if (size == 0L)
|
||||||
|
continue
|
||||||
|
|
||||||
if (isVideo && (isPickImage || showMedia == IMAGES))
|
val name = file.name
|
||||||
continue
|
val path = file.absolutePath
|
||||||
|
val dateModified = file.lastModified()
|
||||||
if (isImage && (isPickVideo || showMedia == VIDEOS))
|
media.add(Medium(name, path, isVideo, dateModified, dateModified, size))
|
||||||
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())
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
cursor?.close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
context.scanFiles(invalidFiles) {}
|
|
||||||
Medium.sorting = mConfig.fileSorting
|
Medium.sorting = mConfig.fileSorting
|
||||||
media.sort()
|
media.sort()
|
||||||
return media
|
return media
|
||||||
|
|
Loading…
Reference in a new issue