diff --git a/imageselector/src/main/java/com/donkingliang/imageselector/ImageSelectorActivity.java b/imageselector/src/main/java/com/donkingliang/imageselector/ImageSelectorActivity.java index 6548462..d9ab5f6 100644 --- a/imageselector/src/main/java/com/donkingliang/imageselector/ImageSelectorActivity.java +++ b/imageselector/src/main/java/com/donkingliang/imageselector/ImageSelectorActivity.java @@ -82,6 +82,7 @@ public class ImageSelectorActivity extends AppCompatActivity { private static final int CAMERA_REQUEST_CODE = 0x00000010; private Uri mCameraUri; private String mCameraImagePath; + private long mTakeTime; private boolean isOpenFolder; private boolean isShowTime; @@ -513,7 +514,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { savePictureUri = Uri.fromFile(new File(mCameraImagePath)); images.add(mCameraImagePath); } - ImageUtil.savePicture(this,savePictureUri); + ImageUtil.savePicture(this,savePictureUri,mTakeTime); saveImageAndFinish(images, true); } else { if (onlyTakePhoto) { @@ -702,6 +703,7 @@ private void openCamera() { captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); startActivityForResult(captureIntent, CAMERA_REQUEST_CODE); + mTakeTime = System.currentTimeMillis(); } } } diff --git a/imageselector/src/main/java/com/donkingliang/imageselector/utils/ImageUtil.java b/imageselector/src/main/java/com/donkingliang/imageselector/utils/ImageUtil.java index 9e30aeb..d67115c 100644 --- a/imageselector/src/main/java/com/donkingliang/imageselector/utils/ImageUtil.java +++ b/imageselector/src/main/java/com/donkingliang/imageselector/utils/ImageUtil.java @@ -312,12 +312,13 @@ public static boolean isCutImage(String dir, String path) { * * @param context * @param uri + * @param takeTime 调起相机拍照的时间 */ - public static void savePicture(final Context context, final Uri uri) { + public static void savePicture(final Context context, final Uri uri, final long takeTime) { new Thread(new Runnable() { @Override public void run() { - if (isNeedSavePicture(context)) { + if (isNeedSavePicture(context, takeTime)) { context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); } } @@ -330,30 +331,29 @@ public void run() { * @param context * @return */ - private static boolean isNeedSavePicture(Context context) { + private static boolean isNeedSavePicture(Context context, long takeTime) { //扫描图片 Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; ContentResolver mContentResolver = context.getContentResolver(); Cursor mCursor = mContentResolver.query(mImageUri, new String[]{ MediaStore.Images.Media.DATE_ADDED, - MediaStore.Images.Media._ID}, - null, + MediaStore.Images.Media._ID, + MediaStore.Images.Media.SIZE}, + MediaStore.MediaColumns.SIZE + ">0", null, MediaStore.Files.FileColumns._ID + " DESC limit 1 offset 0"); //读取扫描到的图片 if (mCursor != null && mCursor.getCount() > 0 && mCursor.moveToFirst()) { //获取图片时间 - long time = mCursor.getLong( - mCursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED)); - + long time = mCursor.getLong(mCursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED)); + int id = mCursor.getInt(mCursor.getColumnIndex(MediaStore.Images.Media._ID)); if (String.valueOf(time).length() < 13) { time *= 1000; } - mCursor.close(); - // 如果照片为30s中内插入的,就认为是拍照图片已插入 - return System.currentTimeMillis() - time > 30 * 1000; + // 如果照片的插入时间大于相机的拍照时间,就认为是拍照图片已插入 + return time + 1000 < takeTime; } return true; }