图像捕获广播在 android 8.0 中无法正常工作
image capture broadcast not working proper in android 8.0
我已经创建了用于接收图像捕获广播的作业服务以标记捕获的图像。毫无疑问,它工作顺利,但现在当我重命名图像或编辑和保存图像或移动或复制它时,它也会执行我不想要的。
我必须检查图像是否来自相机捕获的图像。
如果它来自相机捕获,则只应执行它。
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);
}
}
}
我找到了这个问题的解决方案。
在处理图像之前检查 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;
}
我已经创建了用于接收图像捕获广播的作业服务以标记捕获的图像。毫无疑问,它工作顺利,但现在当我重命名图像或编辑和保存图像或移动或复制它时,它也会执行我不想要的。
我必须检查图像是否来自相机捕获的图像。 如果它来自相机捕获,则只应执行它。
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);
}
}
}
我找到了这个问题的解决方案。 在处理图像之前检查 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;
}