Skip to content

Commit

Permalink
通过调用拍照的时间判断照片是否保存
Browse files Browse the repository at this point in the history
  • Loading branch information
teach committed May 14, 2020
1 parent 10ba97f commit 4c011e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 4c011e0

Please sign in to comment.