android调用系统相机拍摄多张照片

Android通过调用一次系统相机,拍摄多张照片并返回给activity,其实Android时没有提供这样的api的,但是可以通过其他的方式实现这个功能。

思路:在调用系统相机之前记下时间戳,等拍照后去系统图片媒体库中查询图片创建时间大于时间戳的图片。

调用系统拍照,拍多张:

 intent.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
 activity.startActivityForResult(intent, TAKEONCAMERA);

调用系统拍一张的方法是,还有有点区别的:

Intent intentCamera = new Intent();
intentCamera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
activity.startActivityForResult(intentCamera, requestCode);

拍照完毕后返回到activity会回调onActivityResult方法,在方法里进行图片的查找:

    /**
     * 获取拍摄的照片,在调用takeOnCamera之后返回界面时调用该方法。
     */
    public static List<String> getTakePhoto(Context context){

        Cursor cursor = null;
        try {
            cursor = context.getContentResolver()
                    .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, PROJECTION,
                            null, null,
                            MediaStore.Images.Media.DATE_ADDED +  " DESC LIMIT 0,30");
            if (cursor != null){
                List<String> photoList = new ArrayList<>(cursor.getCount());
                cursor.moveToFirst();
                while (!cursor.isAfterLast()){
                    long createTemp = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
                    if (isAfterStart(startTemp, createTemp)){
                        photoList.add(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
                    }
                    cursor.moveToNext();
                }
                return photoList;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (cursor != null){
                cursor.close();
            }
        }
        return null;
    }

完整的代码:

/**
 * -  相册或拍照选取多张照片工具类
 * <p>
 * Created by Sunxy on 2018/9/8.
 */
public class GetPhotoUtils {

    public static final int TAKEONCAMERA = 1002;

    private  static final String[] PROJECTION = {MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATE_ADDED,
            MediaStore.Images.Media.DATA};

    private static long startTemp = Long.MAX_VALUE;
    private static int state = 0;


    /**
     * 得到手机中所有的照片(async)
     */
    public static List<String> getAllPhoto(Context context){

        Cursor cursor = null;
        try {
            cursor = context.getContentResolver()
                    .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, PROJECTION,
                            null, null,
                            MediaStore.Images.Media.DATE_ADDED +  " DESC");
            if (cursor != null){
                List<String> photoList = new ArrayList<>(cursor.getCount());
                cursor.moveToFirst();
                while (!cursor.isAfterLast()){
                    photoList.add(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
                    cursor.moveToNext();
                }
                return photoList;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (cursor != null){
                cursor.close();
            }
        }
        return null;
    }

    /**
     * 调手机相机拍多张照片
     */
    public static void takeOnCamera(Activity activity) {
        //打开相机之前,记录时间1
        startTemp = System.currentTimeMillis();
        Intent intent = new Intent();
        //此处之所以诸多try catch,是因为各大厂商手机不确定哪个方法
        try {
            intent.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
            activity.startActivityForResult(intent, TAKEONCAMERA);
        } catch (Exception e) {
            try {
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE_SECURE);
                activity.startActivityForResult(intent, TAKEONCAMERA);
            } catch (Exception e1) {
                try {
                    intent.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA_SECURE);
                    activity.startActivityForResult(intent, TAKEONCAMERA);
                } catch (Exception ell) {
                    ToastUtils.showToast(activity, "打开摄像机失败,请从相册选择照片");
                }
            }
        }
    }


    /**
     * 获取拍摄的照片,在调用takeOnCamera之后返回界面是调用该方法。
     */
    public static List<String> getTakePhoto(Context context){

        Cursor cursor = null;
        try {
            cursor = context.getContentResolver()
                    .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, PROJECTION,
                            null, null,
                            MediaStore.Images.Media.DATE_ADDED +  " DESC LIMIT 0,30");
            if (cursor != null){
                List<String> photoList = new ArrayList<>(cursor.getCount());
                cursor.moveToFirst();
                while (!cursor.isAfterLast()){
                    long createTemp = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
                    if (isAfterStart(startTemp, createTemp)){
                        photoList.add(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)));
                    }
                    cursor.moveToNext();
                }
                return photoList;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (cursor != null){
                cursor.close();
            }
        }
        return null;
    }

    /**
     * 判断照片创建时间是不是在开始之后,
     * 时间戳可能是10位或者是13位,要先统一位数
     *
     */
    private static boolean isAfterStart(long startTemp, long createTemp){
        if (state == 0){
            int startTempLength = String.valueOf(startTemp).length();
            int createTempLength = String.valueOf(createTemp).length();
            if (startTempLength == createTempLength){
                state = 1;
            }else if(startTempLength == 10 && createTempLength == 13){
                state = 2;
            }else if(startTempLength == 13 && createTempLength == 10){
                state = 3;
            }
        }
        if (state == 2){
            startTemp = startTemp * 1000;
        }else if(state == 3){
            startTemp = startTemp / 1000;
        }
        return createTemp - startTemp >= 0;
    }

}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值