I have created job service for receiving image capture broadcast to stamp the captured image. it working smoothly with no doubt, but now when I'm renaming image or edit and save the image or move or copy it also execute that I don't want.
I have to check whether the image is from camera captured or not. If it is from camera capture then on only it should be executed.
public class JobSchedulerService extends JobService {
private Context mContext;
private static final int ASJOBSERVICE_JOB_ID = 999;
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);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) {
task.execute(intent);
} else {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, intent);
}
}
}
i got the solution for this issue. check ExifInterface data and retrieve date creation before processing the image. if the difference between date of creation and current is more than some period of second than stop processing the image
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;
}