How to get broadcast of new media in gallery?

+1 vote
asked Sep 13, 2017 by adrian-le-roy-devezi

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 need a register that is set in the background to continuously listen or check for new media added to the gallery, and captures the Uri. This used to be done with a broadcast receiver such as:

<receiver
    android:name=".receivers.CameraEventReceiver"
    android:label="CameraEventReceiver">
    <intent-filter>

        <!-- <action android:name="com.android.camera.NEW_PICTURE" /> -->
        <action android:name="android.hardware.action.NEW_PICTURE" />

        <data android:mimeType="image/*" />
    </intent-filter>
</receiver>

However this was deprecated in API level 24. I target API 21-26. Can someone show me how to do this with JobSchedulers like android docs say too?

1 Answer

+1 vote
answered Sep 23, 2017 by amjad-khan

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")
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) context.getSystemService(JobScheduler.class)).schedule(JOB_INFO);
        Log.i("PhotosContentJob", "JOB SCHEDULED!");
        return schedule;
    }

    // Schedule this job, replace any 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("YOUR_PACKAGE_NAME", JobSchedulerService.class.getName()));
            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();
            js.schedule(JOB_INFO);
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public boolean onStartJob(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 = new ArrayList<>();
                for (Uri uri : params.getTriggeredContentUris()) {
                    if (uri != null) {
                        Handler handler = new Handler();
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                //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);
            }
        }
        return true;
    }

    @Override
    public boolean onStopJob(JobParameters params) {
        return false;
    }

}

In Manifest File

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

In Your MainActivity.java which ever your launcher file

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                JobSchedulerService.scheduleJob(this);
    }
}
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
...