Android file monitor

Asked

Viewed 136 times

1

I need to develop an application for android where the same work monitoring a certain directory with a certain periodicity and sending new files from this directory to an FTP or any other folder of the local network that the device is connected. Ex.: Every two minutes monitor the camera directory and whenever a user takes a new photo, this monitor check if there are new photos and send to a local network directory. How to work with this type of app on android?

2 answers

2

To monitor changes in an Android folder use class Fileobserver

A possible implementation would thus be:

public class FileMonitor extends FileObserver {

    public MyFileObserver(String path) {

        super(path, FileObserver.ALL_EVENTS);
    }

    @Override
    public void onEvent(int event, String path) {

        if (path == null) {
            return;
        }

        if ((FileObserver.CREATE & event)!=0) {

            //Foi criado um novo ficheiro ou uma nova pasta.
            //Introduza aqui o código para o tratamento que quer
            //efectuar para este caso 
        }

    }

}

In this example you are only monitoring whether a new file or folder has been created inside the monitored folder.

It is possible to monitor other events, see documentation.

To use do:

FileMonitor monitor = new FileMonitor("/sdcard/minhaPasta/");
monitor.startWatching();

To stop monitoring use:

monitor.stopWatching();
  • ramaral, thank you very much... I have a question: I need the application to run in the background all the time, in fact it doesn’t even have interface, it just needs to do this monitoring... what is the most correct way to do this?

  • Create a service to monitor and define a Broadcastreceiver that is called after the boot of the device. The Broadcastreceiver will be used to start the service.

  • Right... for a better understanding, in this case the service will always run respecting the Filemonitor class or would it be necessary to define a periodicity for execution of it? How would the interaction between the two (Broadcastreceiver and Filemonitor class )?

  • In the service you create an instance of the Filemonitor class and call monitor.startWatching();. Broadcastreceiver will be used to start the service. Broadcastreceiver as I explain here it will launch after starting the device and the service will always be running.

0

You will need a broadcast receiver that will give you information that a new photo has been taken. That is, whenever a photo is taken you will have such information and will call the upload code to FTP

If the photo example is your real problem you can try it: https://stackoverflow.com/questions/4571461/broadcast-receiver-wont-receive-camera-event

Inside your broadcast’s onReceive is where you should list the photos and search for the new photo.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.