如何在Android中使用文件观察器删除文件?

时间:2012-06-07 12:38:22

标签: android file sd-card fileobserver

我正在尝试使用文件Observer删除文件。在我的代码中,如果任何文件被推入SD卡,它将触发一个创建事件。在那个事件上我想删除该文件。我正在尝试下面提供的代码,但我无法删除该文件,因为正在进行文件复制。所以,我现在想要在文件成功复制到SD卡时删除该文件。

将文件成功复制到SD卡时是否有任何事件通知?

    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();

    }
}

请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

据我所知,无法知道文件何时完成复制。但是有一个我能想到的工作。在create事件中,尝试放置一个每3秒检查一次文件完成的timertask。或者尝试每3秒删除一次,直到它不存在为止,然后将timertask从计时器队列中取出。这样,它将继续删除复印机已经完成的工作,直到文件不再存在。

希望有所帮助。