switch to the new way of getting images again, with some tweak later

This commit is contained in:
tibbi 2017-01-11 18:47:49 +01:00
parent 720f2ea312
commit 23d85f6ff9

View file

@ -4,7 +4,7 @@ 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.*
import com.simplemobiletools.gallery.extensions.getLongValue import com.simplemobiletools.gallery.extensions.getLongValue
import com.simplemobiletools.gallery.extensions.getStringValue import com.simplemobiletools.gallery.extensions.getStringValue
import com.simplemobiletools.gallery.helpers.Config import com.simplemobiletools.gallery.helpers.Config
@ -28,51 +28,53 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
val media = ArrayList<Medium>() val media = ArrayList<Medium>()
val invalidFiles = ArrayList<File>() val invalidFiles = ArrayList<File>()
val showMedia = mConfig.showMedia val showMedia = mConfig.showMedia
for (i in 0..1) { val uri = MediaStore.Files.getContentUri("external")
if (i == 0 && (isPickVideo || showMedia == VIDEOS)) val where = "${MediaStore.Images.Media.DATA} LIKE ?"
continue 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
var uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI try {
if (i == 1) { cursor = context.contentResolver.query(uri, columns, where, args, null)
if (isPickImage || showMedia == IMAGES) if (cursor?.moveToFirst() == true) {
continue do {
val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) ?: continue
val file = File(curPath)
val size = cursor.getLongValue(MediaStore.Images.Media.SIZE)
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI if (size == 0L) {
} invalidFiles.add(file)
val where = "${MediaStore.Images.Media.DATA} LIKE ?" continue
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, // exclude images of subdirectories
MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.SIZE) if (!showAll && file.parent != mPath)
var cursor: Cursor? = null continue
try { var name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: ""
cursor = context.contentResolver.query(uri, columns, where, args, null) val isImage = file.isImageFast() || file.isGif()
val isVideo = file.isVideoFast()
if (cursor?.moveToFirst() == true) {
do { if (!isImage && !isVideo)
val curPath = cursor.getStringValue(MediaStore.Images.Media.DATA) ?: continue continue
val file = File(curPath)
val size = cursor.getLongValue(MediaStore.Images.Media.SIZE) if (isVideo && (isPickImage || showMedia == IMAGES))
continue
if (size == 0L) {
invalidFiles.add(file) if (isImage && (isPickVideo || showMedia == VIDEOS))
continue continue
}
if (name.isEmpty())
// exclude images of subdirectories name = curPath.getFilenameFromPath()
if (!showAll && file.parent != mPath) val dateModified = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED)
continue val dateTaken = cursor.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
media.add(Medium(name, curPath, isVideo, dateModified, dateTaken, size))
val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: "" } while (cursor.moveToNext())
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) {} context.scanFiles(invalidFiles) {}