2016-12-03 99 views
0

我是新手。我正在創建一個用作回收站的應用程序。我有一個問題需要幫助。如何在用戶刪除它們之前複製文件 - Android

  • 我使用android File Observer方法,然後我收到文件刪除後通知,然後我將它與路徑複製到我的文件夾。 但文件夾中的新文件爲空

這是我的代碼文件觀察報:

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

    mObservers = new ArrayList<SingleFileObserver>(); 
    Stack<String> stack = new Stack<String>(); 
    stack.push(mPath); 

    while (!stack.isEmpty()) { 
     String parent = 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.ACCESS: 
      Log.d("RecursiveFileObserver", "ACCESS: " + path); 
      break; 
     case FileObserver.ATTRIB: 
      Log.i("RecursiveFileObserver", "ATTRIB: " + path); 
      break; 
     case FileObserver.CLOSE_NOWRITE: 
      Log.i("RecursiveFileObserver", "CLOSE_NOWRITE: " + path); 
      break; 
     case FileObserver.CLOSE_WRITE: 
      Log.i("RecursiveFileObserver", "CLOSE_WRITE: " + path); 
      break; 
     case FileObserver.CREATE: 
      Log.i("RecursiveFileObserver", "CREATE: " + path); 
      break; 
     case FileObserver.DELETE: 
      Log.e("Save:", "Save file user delete"); 

      File dir = new File(Environment.getExternalStorageDirectory() + File.separator + ConstantUtils.FOLDER_RESTORE); 
      if (!dir.exists()){ 
       dir.mkdir(); 

      } 
      File inputFile = new File(path); 
      String str = String.valueOf(inputFile); 
      if (!str.isEmpty()){ 
       String [] result = str.split("/"); 
       int i = result.length; 
       if (i > 4) { 
        String inputPath = result[i - 1]; 
        File outputFile = new File(Environment.getExternalStorageDirectory() + File.separator + ConstantUtils.FOLDER_RESTORE + File.separator + inputPath); 
        String str1 = String.valueOf(inputFile); 
        String str2 = String.valueOf(outputFile); 
        if (str1.compareTo(str2) != 0){ 
         ZipHelper.copyFileToFolder(inputFile, outputFile); 
        } 
       } 
      } 
      break; 
     case FileObserver.DELETE_SELF: 
      Log.i("RecursiveFileObserver", "DELETE_SELF: " + path); 
      break; 
     case FileObserver.MODIFY: 
      Log.i("RecursiveFileObserver", "MODIFY: " + path); 
      break; 
     case FileObserver.MOVE_SELF: 
      Log.i("RecursiveFileObserver", "MOVE_SELF: " + path); 
      break; 
     case FileObserver.MOVED_FROM: 
      Log.i("RecursiveFileObserver", "MOVED_FROM: " + path); 
      break; 
     case FileObserver.MOVED_TO: 
      Log.i("RecursiveFileObserver", "MOVED_TO: " + path); 
      break; 
     case FileObserver.OPEN: 
      Log.i("RecursiveFileObserver", "OPEN: " + path); 
      break; 
     default: 
      Log.i("RecursiveFileObserver", "DEFAULT(" + event + "): " + path); 
      break; 
    } 
} 

請告訴我,我怎麼能做到這一點。對不起,我的英文不好。非常感謝!

回答

0

FileObserver.DELETE被觸發後的文件被刪除這就是爲什麼你不能複製它了。顯然,FileObserver中沒有事件告訴你何時將要刪除一個文件。

一個非常愚蠢的方法可能是最初的所有文件複製裏面的mpath一些緩存目錄,然後監視FileObserver.DELETE。然後,您可以在數據庫中標記已刪除的文件,但仍然允許用戶通過將緩存的文件複製回目錄來恢復其刪除。

這可能是好的,如果只有少數文件,但肯定不是當目錄相當大時。

+0

感謝您的幫助。但我不想那樣。 – TuNguyen

相關問題