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

View file

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

View file

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