android 模仿微信输入时获取最新一张截图或拍照相片

版权声明:转载时请标明出处。https://blog.csdn.net/mhhyoucom https://blog.csdn.net/mhhyoucom/article/details/82145879

首先看看这张图片:
这里写图片描述
微信有个很好用户体验功能就是在输入时候提示你刚刚拍照的相片或者是截图的图片,可以快速输入。
实际上是通过内容提供者来从截图或者拍照文件中获取最新的图片进行时间对比来选择最新一张,我们来看看代码实现。
已经封装成工具类直接可以使用:

package com.chengxing.cxsdk;

import android.content.Context;
import android.database.Cursor;
import android.os.Environment;
import android.provider.MediaStore;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class GetImgUtils {

    public static final String cameraPath="/DCIM/Camera";
    public static final String screentShotsPath="/DCIM/Screenshots";
    public static final String screentPicturePath="/Pictures/Screenshots";

    public static class ImgBean{
        public long mTime;
        public String imgUrl;

        public ImgBean(long mTime, String imgUrl) {
            this.mTime = mTime;
            this.imgUrl = imgUrl;
        }

        @Override
        public String toString() {
            return "ImgBean{" +
                    "mTime=" + mTime +
                    ", imgUrl='" + imgUrl + '\'' +
                    '}';
        }
    }

    public static List<ImgBean> getLatestPhoto(Context context, int limit) {
        //拍摄照片的地址
        String CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString() + cameraPath;
        //截屏照片的地址
        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文件夹,查询并排序
        List<ImgBean> imgBeans=new ArrayList<>();
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection,
                selection,
                selectionArgs,
                MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");
        int index=0;
        while (cursor.moveToNext()) {
            if (index>=limit)
                break;
            long mtime=cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED));
            String imgUrl=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            imgBeans.add(new ImgBean(mtime, imgUrl));
            index++;
        }
        if (!cursor.isClosed()) {
            cursor.close();
        }
        //检查Screenshots文件夹
        //查询并排序
        cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection,
                selection,
                selectionArgsForScreenshots,
                MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC");

        index=0;
        while (cursor.moveToNext()) {
            if (index>=limit)
                break;
            long mtime=cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED));
            String imgUrl=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            imgBeans.add(new ImgBean(mtime, imgUrl));
            index++;
        }
        if (!cursor.isClosed()) {
            cursor.close();
        }
        //排序对比时间
        Collections.sort(imgBeans, new Comparator<ImgBean>() {
            @Override
            public int compare(ImgBean imgBean, ImgBean t1) {
                return (int) (t1.mTime-imgBean.mTime);
            }
        });
        List<ImgBean> imgBeans1=new ArrayList<>();
        index=0;
        for (ImgBean imgBean : imgBeans) {
            if (index>=limit)
                break;
            imgBeans1.add(imgBean);
            index++;
        }
        return imgBeans1;
    }

    /**
     * 获取相册中最新一张图片
     *
     * @param context
     * @return
     */
    public static List<ImgBean> getLatestPhoto(Context context) {
        return getLatestPhoto(context,1);
    }

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

    /**
     * 获取截图路径
     * @return
     */
    public static String getScreenshotsPath(){
        String path = Environment.getExternalStorageDirectory().toString() + screentShotsPath;
        File file = new File(path);
        if(!file.exists()){
            path = Environment.getExternalStorageDirectory().toString() + screentPicturePath;
        }
        file = null;
        return path;
    }
}

使用:
调用getLatestPhoto方法即可。

对你有用点个赞哦!!!
插件化项目:https://github.com/jasonliyihang/speed_tools

猜你喜欢

转载自blog.csdn.net/mhhyoucom/article/details/82145879