go back to the mediastore way of fetching media

This commit is contained in:
tibbi 2017-05-20 09:34:59 +02:00
parent b2f9bbdf2c
commit cc814d8d45

View file

@ -2,9 +2,8 @@ package com.simplemobiletools.gallery.asynctasks
import android.content.Context import android.content.Context
import android.os.AsyncTask import android.os.AsyncTask
import com.simplemobiletools.commons.extensions.isGif import android.provider.MediaStore
import com.simplemobiletools.commons.extensions.isImageFast import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.extensions.isVideoFast
import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.getParents import com.simplemobiletools.gallery.extensions.getParents
import com.simplemobiletools.gallery.helpers.IMAGES import com.simplemobiletools.gallery.helpers.IMAGES
@ -45,37 +44,63 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
return Unit return Unit
} }
private fun getFilesFrom(path: String) { private fun getFilesFrom(curPath: String) {
val dir = File(path) val projection = arrayOf(MediaStore.Images.Media._ID,
val filenames = dir.list() ?: return MediaStore.Images.Media.DISPLAY_NAME,
for (filename in filenames) { MediaStore.Images.Media.DATE_TAKEN,
if (shouldStop) MediaStore.Images.Media.DATE_MODIFIED,
cancel(true) MediaStore.Images.Media.DATA,
MediaStore.Images.Media.SIZE)
val uri = MediaStore.Files.getContentUri("external")
val selection = "${MediaStore.Images.Media.DATA} LIKE ? AND ${MediaStore.Images.Media.DATA} NOT LIKE ?"
val selectionArgs = arrayOf("$curPath/%", "$curPath/%/%")
val isImage = filename.isImageFast() || filename.isGif() val cur = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
val isVideo = if (isImage) false else filename.isVideoFast() if (cur.moveToFirst()) {
var filename: String
var path: String
var dateTaken: Long
var dateModified: Long
var size: Long
if (!isImage && !isVideo) do {
continue if (shouldStop)
cancel(true)
if (isVideo && (isPickImage || showMedia == IMAGES)) path = cur.getStringValue(MediaStore.Images.Media.DATA)
continue size = cur.getLongValue(MediaStore.Images.Media.SIZE)
if (size == 0L) {
size = File(path).length()
}
if (isImage && (isPickVideo || showMedia == VIDEOS)) if (size <= 0L) {
continue continue
}
if (!showHidden && filename.startsWith('.')) filename = cur.getStringValue(MediaStore.Images.Media.DISPLAY_NAME) ?: ""
continue val isImage = filename.isImageFast() || filename.isGif()
val isVideo = if (isImage) false else filename.isVideoFast()
val file = File(path, filename) if (!isImage && !isVideo)
val size = file.length() continue
if (size == 0L)
continue
val dateModified = file.lastModified() if (isVideo && (isPickImage || showMedia == IMAGES))
val medium = Medium(filename, file.absolutePath, isVideo, dateModified, dateModified, size) continue
media.add(medium)
if (isImage && (isPickVideo || showMedia == VIDEOS))
continue
if (!showHidden && filename.startsWith('.'))
continue
dateTaken = cur.getLongValue(MediaStore.Images.Media.DATE_TAKEN)
dateModified = cur.getIntValue(MediaStore.Images.Media.DATE_MODIFIED) * 1000L
val medium = Medium(filename, path, isVideo, dateModified, dateTaken, size)
media.add(medium)
} while (cur.moveToNext())
} }
cur.close()
} }
override fun onPostExecute(result: Unit?) { override fun onPostExecute(result: Unit?) {