我创建了作业服务来接收广播的图像捕获以标记捕获的图像。毫无疑问,它运行顺利,但是现在当我重命名图像或编辑并保存图像或移动或复制图像时,它也会执行我不想要的操作。
我必须检查图像是否来自捕获的相机。 如果是从摄像机捕获的,则仅应执行。
public class JobSchedulerService extends JobService {
private Context mContext;
private static final int ASJOBSERVICE_JOB_ID = 999;
// A pre-built JobInfo we use for scheduling our job.
private static JobInfo JOB_INFO = null;
public static int a(Context context) {
int schedule = ((JobScheduler) Objects.requireNonNull(context.getSystemService(JobScheduler.class))).schedule(JOB_INFO);
Log.i("PhotosContentJob", "JOB SCHEDULED!");
return schedule;
}
// Schedule this job, replace an existing one.
public static void scheduleJob(Context context) {
if (JOB_INFO != null) {
a(context);
} else {
JobScheduler js = context.getSystemService(JobScheduler.class);
JobInfo.Builder builder = new JobInfo.Builder(ASJOBSERVICE_JOB_ID, new ComponentName(BuildConfig.APPLICATION_ID, JobSchedulerService.class.getName()));
// builder.addTriggerContentUri(new JobInfo.TriggerContentUri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, JobInfo.TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS));
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1));
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(MediaStore.Images.Media.INTERNAL_CONTENT_URI, 1));
builder.setTriggerContentMaxDelay(500);
JOB_INFO = builder.build();
if (js != null) {
js.schedule(JOB_INFO);
}
}
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public boolean onStartJob(final JobParameters params) {
mContext = this;
if (params.getTriggeredContentAuthorities() != null) {
if (params.getTriggeredContentUris() != null) {
ArrayList<String> ids = new ArrayList<>();
for (Uri uri : params.getTriggeredContentUris()) {
if (uri != null) {
final Intent i = new Intent(mContext, ImageCaptureBroadCastReceiver.class);
i.setData(uri);
Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
doTheTask(new StampImageAsync(mContext.getApplicationContext()), i);
}
});
}
}
jobFinished(params, true); // see this, we are saying we just finished the job
scheduleJob(this);
}
}
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
private void doTheTask(StampImageAsync task, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Android 4.4 (API 19) and above
task.execute(intent);
} else {
// Android 3.0 to Android 4.3
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, intent);
}
}
}
答案 0 :(得分:0)
我找到了解决此问题的方法。 在处理图像之前,请检查ExifInterface数据并检索创建日期。 如果创建日期与当前日期之间的差值大于某个秒数,则停止处理图像
if(checkExif(imagePath){
//do image processing
}
private boolean checkExif(String path){
ExifInterface ei = null;
String dateTime="";
String dateTimeDigi="";
String dateTimeOrig="";
try {
ei = new ExifInterface(path);
} catch (IOException e) {
Log.e("Exception Exif ",""+e);
return false;
}
try {
if (ei != null) {
dateTime=ei.getAttribute(ExifInterface.TAG_DATETIME);
Log.e("dateTime",""+dateTime);
}
} catch (Exception e) {
Log.e("Exception Exif ",""+e);
dateTime="";
}
Date current = Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:dd hh:mm:ss", Locale.getDefault());
String formattedDate = dateFormat.format(current);
Log.e("dateTime Cur ",""+formattedDate );
Date startDate, endDate;
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy:MM:dd hh:mm:ss");
try {
startDate = simpleDateFormat.parse(dateTime);
endDate = simpleDateFormat.parse(formattedDate);
}catch (ParseException e){
return false;
}
long dif=getDifference(startDate,endDate);
Log.e("getDifference 001",""+dif );
if(dif<15){
return true;
}
return false;
}
private long getDifference(Date startDate ,Date endDate) {
//milliseconds
long different = endDate.getTime() - startDate.getTime();
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
if(elapsedDays==0 && elapsedHours==0 && elapsedMinutes==0) return elapsedSeconds;
else return 500;
}