some updates to the way we get media from mediastore

This commit is contained in:
tibbi 2016-12-04 17:52:10 +01:00
parent c9eb50944d
commit c9c5f549be
4 changed files with 64 additions and 47 deletions

View file

@ -28,7 +28,6 @@ import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.activity_medium.*
import java.io.File
import java.util.*
import java.util.regex.Pattern
class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View.OnSystemUiVisibilityChangeListener, ViewPagerFragment.FragmentClickListener {
private var mMedia: MutableList<Medium>? = null
@ -86,13 +85,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
if (isDirEmpty())
return
val pagerAdapter = MyPagerAdapter(this, supportFragmentManager, mMedia!!)
view_pager.apply {
adapter = pagerAdapter
currentItem = mPos
addOnPageChangeListener(this@ViewPagerActivity)
}
updatePagerItems()
window.decorView.setOnSystemUiVisibilityChangeListener(this)
updateActionbarTitle()
undo_delete.setOnClickListener { undoDeletion() }
@ -165,6 +158,15 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
adapter.updateItems(mPos)
}
private fun updatePagerItems() {
val pagerAdapter = MyPagerAdapter(this, supportFragmentManager, mMedia!!)
view_pager.apply {
adapter = pagerAdapter
currentItem = mPos
addOnPageChangeListener(this@ViewPagerActivity)
}
}
private fun displayCopyDialog() {
val files = ArrayList<File>()
files.add(getCurrentFile())
@ -311,33 +313,36 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
val where = "${MediaStore.Images.Media.DATA} LIKE ? "
val args = arrayOf("$mDirectory%")
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.SIZE)
val cursor = contentResolver.query(uri, columns, where, args, null)
val pattern = "${Pattern.quote(mDirectory)}/[^/]*"
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.SIZE)
var cursor: Cursor? = null
try {
cursor = contentResolver.query(uri, columns, where, args, null)
if (cursor?.moveToFirst() == true) {
val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
do {
val curPath = cursor.getString(pathIndex) ?: continue
if (curPath != mToBeDeleted && curPath != mBeingDeleted) {
val file = File(curPath)
if (!file.exists()) {
invalidFiles.add(file)
if (file.exists()) {
if (file.parent != mDirectory)
continue
val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME)
val timestamp = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED)
val size = cursor.getLongValue(MediaStore.Images.Media.SIZE)
media.add(Medium(name, curPath, i == 1, timestamp, size))
} else {
invalidFiles.add(file)
}
if (curPath.matches(pattern.toRegex()) && curPath != mToBeDeleted && curPath != mBeingDeleted) {
val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)
val timestamp = cursor.getLong(dateIndex)
val sizeIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE)
val size = cursor.getLong(sizeIndex)
media.add(Medium(file.name, curPath, i == 1, timestamp, size))
}
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
}
scanFiles(invalidFiles) {}
Medium.sorting = mConfig.sorting

View file

@ -5,11 +5,12 @@ import android.database.Cursor
import android.os.AsyncTask
import android.provider.MediaStore
import com.simplemobiletools.filepicker.extensions.scanFiles
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.getHumanizedFilename
import com.simplemobiletools.gallery.extensions.getLongValue
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.helpers.SORT_BY_NAME
import com.simplemobiletools.gallery.helpers.SORT_DESCENDING
import com.simplemobiletools.gallery.extensions.getHumanizedFilename
import com.simplemobiletools.gallery.models.Directory
import java.io.File
import java.util.*
@ -37,16 +38,17 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI
}
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED)
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.SIZE)
val order = getSortOrder()
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, columns, null, null, order)
if (cursor != null && cursor.moveToFirst()) {
if (cursor?.moveToFirst() == true) {
val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
do {
val fullPath: String = cursor.getString(pathIndex) ?: continue
val fullPath = cursor.getString(pathIndex) ?: continue
val file = File(fullPath)
val parentDir = file.parent
@ -55,8 +57,6 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
continue
}
val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)
val timestamp = cursor.getLong(dateIndex)
if (directories.containsKey(parentDir)) {
val directory: Directory = directories[parentDir]!!
val newImageCnt = directory.mediaCnt + 1
@ -68,7 +68,9 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
dirName += " ${context.resources.getString(R.string.hidden)}"
}
directories.put(parentDir, Directory(parentDir, fullPath, dirName, 1, timestamp, file.length()))
val timestamp = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED)
val size = cursor.getLongValue(MediaStore.Images.Media.SIZE)
directories.put(parentDir, Directory(parentDir, fullPath, dirName, 1, timestamp, size))
}
} while (cursor.moveToNext())
}

View file

@ -5,11 +5,12 @@ import android.database.Cursor
import android.os.AsyncTask
import android.provider.MediaStore
import com.simplemobiletools.filepicker.extensions.scanFiles
import com.simplemobiletools.gallery.extensions.getLongValue
import com.simplemobiletools.gallery.extensions.getStringValue
import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.models.Medium
import java.io.File
import java.util.*
import java.util.regex.Pattern
class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo: Boolean, val isPickImage: Boolean,
val mToBeDeleted: List<String>, val callback: (media: ArrayList<Medium>) -> Unit) : AsyncTask<Void, Void, ArrayList<Medium>>() {
@ -36,24 +37,26 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickVideo
}
val where = "${MediaStore.Images.Media.DATA} LIKE ?"
val args = arrayOf("$mPath%")
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED)
val pattern = "${Pattern.quote(mPath)}/[^/]*"
val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.SIZE)
var cursor: Cursor? = null
try {
cursor = context.contentResolver.query(uri, columns, where, args, null)
if (cursor != null && cursor.moveToFirst()) {
if (cursor?.moveToFirst() == true) {
val pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA)
do {
val curPath = cursor.getString(pathIndex) ?: continue
if (curPath.matches(pattern.toRegex()) && !mToBeDeleted.contains(curPath)) {
if (!mToBeDeleted.contains(curPath)) {
val file = File(curPath)
if (file.exists()) {
val dateIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)
val timestamp = cursor.getLong(dateIndex)
media.add(Medium(file.name, curPath, i == 1, timestamp, file.length()))
if (file.parent != mPath)
continue
val name = cursor.getStringValue(MediaStore.Images.Media.DISPLAY_NAME)
val timestamp = cursor.getLongValue(MediaStore.Images.Media.DATE_MODIFIED)
val size = cursor.getLongValue(MediaStore.Images.Media.SIZE)
media.add(Medium(name, curPath, i == 1, timestamp, size))
} else {
invalidFiles.add(file)
}

View file

@ -0,0 +1,7 @@
package com.simplemobiletools.gallery.extensions
import android.database.Cursor
fun Cursor.getStringValue(key: String) = getString(getColumnIndex(key))
fun Cursor.getLongValue(key: String) = getLong(getColumnIndex(key))