From 4c011e054832a07ba3749c4170d9f7c2361588e1 Mon Sep 17 00:00:00 2001 From: teach Date: Thu, 14 May 2020 16:09:45 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E8=BF=87=E8=B0=83=E7=94=A8=E6=8B=8D?= =?UTF-8?q?=E7=85=A7=E7=9A=84=E6=97=B6=E9=97=B4=E5=88=A4=E6=96=AD=E7=85=A7?= =?UTF-8?q?=E7=89=87=E6=98=AF=E5=90=A6=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../imageselector/ImageSelectorActivity.java | 4 +++- .../imageselector/utils/ImageUtil.java | 22 +++++++++---------- 2 files changed, 14 insertions(+), 12 deletions(-) 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; }