1

我正在尝试使用文件观察者删除文件。在我的代码中,如果任何文件被推入 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();

    }
}

请帮我。提前致谢。

4

1 回答 1

0

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.

于 2012-06-30T22:12:43.820 回答