revert to the previous way of obtaining images

This commit is contained in:
tibbi 2017-01-10 18:22:39 +01:00
parent e94516746d
commit 271fa72abb

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