package com.zxhy.hfilemanagermaster.knife

import android.annotation.SuppressLint
import android.app.Application
import android.database.Cursor
import android.provider.MediaStore
import java.lang.Exception

/**
 * 最近图片
 * 无效
 */
@SuppressLint("Recycle")
fun Application.getRecentPhoto() {
    val list = arrayListOf<String>()
    var cursor: Cursor? = null
    val currentTime = System.currentTimeMillis() / 1000 - 60

    try {
        cursor = this.applicationContext.contentResolver.query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            arrayOf(MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA),
            MediaStore.Images.Media.DATE_ADDED + " >= ?", arrayOf("$currentTime"),
            MediaStore.Images.Media.DATE_ADDED + " DESC"
        )
        if ((cursor?.count ?: 0) < 1) {
            cursor?.close()
        }
        if (cursor?.moveToFirst() == true) {
            val path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA))
            list.add(path)
        }
    } catch (e: Exception) {
        e.printStackTrace()
    } finally {
        cursor?.close()
    }
}

