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;
}
}
請告訴我,我怎麼能做到這一點。對不起,我的英文不好。非常感謝!
感謝您的幫助。但我不想那樣。 – TuNguyen