Skip to content Skip to sidebar Skip to footer

How To Get Broadcast Of New Media In Gallery?

I am trying to get notified of a new picture or video added to the phone gallery. I would need to obtain the new media's URI. The purpose is so i can automatically back it up. So i

Solution 1:

In API level 23 above Job scheduling is been introduce to perform the task

So I am placing sample of Job Scheduling which can help you

JobSchedulerService.java

@SuppressLint("NewApi")publicclassJobSchedulerServiceextendsJobService {

    private Context mContext;
    privatestaticfinalintASJOBSERVICE_JOB_ID=999;

    // A pre-built JobInfo we use for scheduling our job.privatestaticJobInfoJOB_INFO=null;

    publicstaticinta(Context context) {
        intschedule= ((JobScheduler) context.getSystemService(JobScheduler.class)).schedule(JOB_INFO);
        Log.i("PhotosContentJob", "JOB SCHEDULED!");
        return schedule;
    }

    // Schedule this job, replace any existing one.publicstaticvoidscheduleJob(Context context) {
        if (JOB_INFO != null) {
            a(context);
        } else {
            JobSchedulerjs= context.getSystemService(JobScheduler.class);
            JobInfo.Builderbuilder=newJobInfo.Builder(ASJOBSERVICE_JOB_ID,
                    newComponentName(BuildConfig.APPLICATION_ID, JobSchedulerService.class.getName()));
            builder.addTriggerContentUri(newJobInfo.TriggerContentUri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1));
            builder.addTriggerContentUri(newJobInfo.TriggerContentUri(MediaStore.Images.Media.INTERNAL_CONTENT_URI, 1));
            builder.setTriggerContentMaxDelay(500);
            JOB_INFO = builder.build();
            js.schedule(JOB_INFO);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)@OverridepublicbooleanonStartJob(final JobParameters params) {
        mContext = this;
        // Did we trigger due to a content change?if (params.getTriggeredContentAuthorities() != null) {
            if (params.getTriggeredContentUris() != null) {
                // If we have details about which URIs changed, then iterate through them// and collect either the ids that were impacted or note that a generic// change has happened.
                ArrayList<String> ids = newArrayList<>();
                for (Uri uri : params.getTriggeredContentUris()) {
                    if (uri != null) {
                        Handlerhandler=newHandler();
                        handler.post(newRunnable() {
                            @Overridepublicvoidrun() {
                                //Perform your task here
                            }
                        });
                    }
                }
                jobFinished(params, true); // see this, we are saying we just finished the job// We will emulate taking some time to do this work, so we can see batching happen.
                scheduleJob(this);
            }
        }
        returntrue;
    }

    @OverridepublicbooleanonStopJob(JobParameters params) {
        returnfalse;
    }

}

In Manifest File

<serviceandroid:name=".services.JobSchedulerService"android:enabled="true"android:permission="android.permission.BIND_JOB_SERVICE" />

In Your MainActivity.java which ever your launcher file

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                JobSchedulerService.scheduleJob(this);
    }
}

Post a Comment for "How To Get Broadcast Of New Media In Gallery?"