Android calls the system camera once, takes multiple photos and returns them to activity. In fact, Android does not provide such an api, but it can achieve this function in other ways.
Idea: write down the time stamp before calling the system camera, and then go to the system picture media library to query the pictures whose creation time is greater than the time stamp after taking photos.
Call the system to take photos and take multiple photos:
intent.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); activity.startActivityForResult(intent, TAKEONCAMERA);
There are some differences in calling the system to take a picture:
Intent intentCamera = new Intent(); intentCamera.setAction(MediaStore.ACTION_IMAGE_CAPTURE); intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); activity.startActivityForResult(intentCamera, requestCode);
After taking a picture, return to activity and call back the onActivityResult method to find the picture in the method:
/** * Get the photos taken, and call this method when you return to the interface after calling 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; }
Complete code:
/** * - Photo album or photo select multiple Photo Tools * <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; /** * Get all the photos in your phone (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; } /** * Take multiple photos with mobile camera */ public static void takeOnCamera(Activity activity) { //Record time 1 before turning on the camera startTemp = System.currentTimeMillis(); Intent intent = new Intent(); //The reason why there are many try catch es here is that mobile phones of major manufacturers are not sure which method to use 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, "Failed to open camera, please select photo from album"); } } } } /** * Get the photos taken, and call this method after calling 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; } /** * Determine whether the photo creation time is after the start, * The time stamp may be 10 or 13 bits, and the number of digits should be unified first * */ 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; } }