Android 获得最近一张拍照或截屏图片

类型微信那种,拍了一张照片或者截了屏 点击聊天加号 就会有个 弹框询问是否要发送那张图片;这里只是做了获取那张最新图片的代码,没有判断时间间隔

Kotlin版

/**
 * Created by JokAr on 2017/10/16.
 */
object ImageUtil {

    /**
     * 返回相册或截屏中最新的一张图片
     */
    fun getLatestPhoto(context: Context): Pair<Long, String>? {
        //拍摄照片的地址
        val CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera"
        //截屏照片的地址
        val SCREENSHOTS_IMAGE_BUCKET_NAME = getScreenshotsPath()
        //拍摄照片的地址ID
        val CAMERA_IMAGE_BUCKET_ID = getBucketId(CAMERA_IMAGE_BUCKET_NAME)
        //截屏照片的地址ID
        val SCREENSHOTS_IMAGE_BUCKET_ID = getBucketId(SCREENSHOTS_IMAGE_BUCKET_NAME)
        //查询路径和修改时间
        val projection = arrayOf(MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DATE_MODIFIED)
        val selection = MediaStore.Images.Media.BUCKET_ID + " = ?"

        val selectionArgs = arrayOf(CAMERA_IMAGE_BUCKET_ID)
        val selectionArgsForScreenshots = arrayOf(SCREENSHOTS_IMAGE_BUCKET_ID)

        var cameraPair: Pair<Long, String>? = null
        //检查camera文件夹,查询并排序
        var cursor = context.contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection,
                selection,
                selectionArgs,
                MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC")
        if (cursor.moveToFirst()) {
            cameraPair = Pair(cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)))

        }

        //检查Screenshots文件夹
        var screenshotsPair: Pair<Long, String>? = null
        //查询并排序
        cursor = context.contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection,
                selection,
                selectionArgsForScreenshots,
                MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC")
        if (cursor.moveToFirst()) {
            screenshotsPair = Pair(cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)))
        }

        if (!cursor.isClosed) {
            cursor.close()
        }
        //对比
        if (cameraPair != null && screenshotsPair != null) {
            return if (cameraPair.first!! > screenshotsPair.first!!) {
                screenshotsPair = null
                cameraPair
            } else {
                cameraPair = null
                screenshotsPair
            }

        } else if (cameraPair != null && screenshotsPair == null) {
            return cameraPair

        } else if (cameraPair == null && screenshotsPair != null) {
            return screenshotsPair
        }

        return null
    }

    private fun getBucketId(path: String): String {
        return path.toLowerCase().hashCode().toString()
    }

     /**
     * 获取截图路径
     */
    private fun getScreenshotsPath(): String {
        var path = Environment.getExternalStorageDirectory().toString() + "/DCIM/Screenshots"
        var file: File? = File(path)
        if (!file?.exists()!!) {
            path = Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots"
        }
        file = null
        return path
    }
}

Java版

  /**
     * 获取相册中最新一张图片
     *
     * @param context
     * @return
     */
    public static Pair<Long, String> getLatestPhoto(Context context) {
        //拍摄照片的地址
        String CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString() + "/DCIM/Camera";
        //截屏照片的地址
        String SCREENSHOTS_IMAGE_BUCKET_NAME = getScreenshotsPath();
        //拍摄照片的地址ID
        String CAMERA_IMAGE_BUCKET_ID = getBucketId(CAMERA_IMAGE_BUCKET_NAME);
        //截屏照片的地址ID
        String SCREENSHOTS_IMAGE_BUCKET_ID = getBucketId(SCREENSHOTS_IMAGE_BUCKET_NAME);
        //查询路径和修改时间
        String[] projection = {MediaStore.Images.Media.DATA,
                MediaStore.Images.Media.DATE_MODIFIED};
        //
        String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
        //
        String[] selectionArgs = {CAMERA_IMAGE_BUCKET_ID};
        String[] selectionArgsForScreenshots = {SCREENSHOTS_IMAGE_BUCKET_ID};

        //检查camera文件夹,查询并排序
        Pair<Long, String> cameraPair = null;
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection,
                selection,
                selectionArgs,
                MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
        if (cursor.moveToFirst()) {
            cameraPair = new Pair(cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
        }
        //检查Screenshots文件夹
        Pair<Long, String> screenshotsPair = null;
        //查询并排序
        cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection,
                selection,
                selectionArgsForScreenshots,
                MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");

        if (cursor.moveToFirst()) {
            screenshotsPair = new Pair(cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED)),
                    cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
        }
        if (!cursor.isClosed()) {
            cursor.close();
        }
        //对比
        if (cameraPair != null && screenshotsPair != null) {
            if (cameraPair.first > screenshotsPair.first) {
                screenshotsPair = null;
                return cameraPair;
            } else {
                cameraPair = null;
                return screenshotsPair;
            }

        } else if (cameraPair != null && screenshotsPair == null) {
            return cameraPair;

        } else if (cameraPair == null && screenshotsPair != null) {
            return screenshotsPair;
        }
        return null;
    }

 private static String getBucketId(String path) {
        return String.valueOf(path.toLowerCase().hashCode());
    }

 /**
     * 获取截图路径
     * @return
     */
    public static String getScreenshotsPath(){
        String path = Environment.getExternalStorageDirectory().toString() + "/DCIM/Screenshots";
        File file = new File(path);
        if(!file.exists()){
            path = Environment.getExternalStorageDirectory().toString() + "/Pictures/Screenshots";
        }
        file = null;
        return path;
    }
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值