How to delete file using file observer in Android?

1.8k views Asked by At

I am trying to delete file using file Observer. In my code, if any file is pushed into SD card, it fires a create event. On that event I want to delete that file. I am trying the code provided below but I am unable to delete the file because the file copying is taking place. So, I now want to delete the file when the file is successfully copied into SD card.

Are there any notification of event fired when file is copied successfully into SD card?

    public class RecursiveFileObserver extends FileObserver
{
    /** Only modification events */
    public static int CHANGES_ONLY = CREATE | DELETE | CLOSE_WRITE | MOVE_SELF
    | MOVED_FROM | MOVED_TO;

    List<SingleFileObserver> mObservers;
    String mPath;
    int mMask;
    Context mContext;

    public RecursiveFileObserver(String path, Context context)
    {
        this(path, ALL_EVENTS, context);
    }

    public RecursiveFileObserver(String path, int mask, Context context)
    {
        super(path, mask);
        mPath = path;
        mMask = mask;
        mContext = context;
    }

    @Override 
    public void startWatching()
    {
        if (mObservers != null)
            return ;

        mObservers = new ArrayList();
        Stack stack = new Stack();
        stack.push(mPath);

        while (!stack.isEmpty())
        {
            String parent = String.valueOf(stack.pop());
            mObservers.add(new SingleFileObserver(parent, mMask));
            File path = new File(parent);
            File[]files = path.listFiles();
            if (null == files)
            continue;
            for (File f: files)
            {
                if (f.isDirectory() && !f.getName().equals(".") && !f.getName().equals(".."))
                {
                    stack.push(f.getPath());
                }
            }
        }

        for (SingleFileObserver sfo: mObservers)
        {
            sfo.startWatching();
        }
    }

    @Override 
    public void stopWatching()
    {
        if (mObservers == null)
            return ;

        for (SingleFileObserver sfo: mObservers)
        {
            sfo.stopWatching();
        }
        mObservers.clear();
        mObservers = null;
    }

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

            case FileObserver.CREATE:
            Log.i("RecursiveFileObserver", "CREATE: " + path);
            File file = new File(path);
            Log.d("File length "," file length = "+file.length());

            if(file.exists()){
            boolean deleted = file.delete(); 
            Log.d("File Deteted "," file Delete = "+deleted);
            }
            break;

        }
    }

    /**
    * Monitor single directory and dispatch all events to its parent, with full path.
    */
    class SingleFileObserver extends FileObserver
    {
        String mPath;

        public SingleFileObserver(String path)
        {
            this(path, ALL_EVENTS);
            mPath = path;
        }

        public SingleFileObserver(String path, int mask)
        {
            super(path, mask);
            mPath = path;
        }

        @Override public void onEvent(int event, String path)
        {
            String newPath = mPath + "/" + path;
            RecursiveFileObserver.this.onEvent(event, newPath);
        }
    }
}


    public class TestFileObserverActivity extends Activity {

    RecursiveFileObserver mObserver;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mObserver = new RecursiveFileObserver(Environment.getExternalStorageDirectory().getAbsolutePath(), RecursiveFileObserver.CREATE, this);
        mObserver.startWatching();

        Toast.makeText(getApplicationContext(), "Watching " + Environment.getExternalStorageDirectory().getAbsolutePath(), Toast.LENGTH_LONG).show();

    }
}

Please help me. Thanks in advance.

1

There are 1 answers

2
Osmium USA On

To my knowledge there is no way of knowing when a file has finished copying. But there is a work around that I can think of. In the create event, try putting a timertask that checks for file completion every 3 seconds. Or try deleting it every 3 seconds until it doesn't exist, then take the timertask off the timer queue. That way it will keep deleting what the copier has done until the file no longer exists.

Hope that helps.