Android 删除sdcard目录中的某些目录文件

1、通过FileFilter()方法找到要删除的目录,如:

			//返回要删除的目录列表
			File[] childFiles = part.listFiles(new FileFilter() {
				
				@Override
				public boolean accept(File pathname) {
					String name = pathname.getName().toLowerCase();
					Logger.d("accept---------------name = " + name);
					return name.equals(delete_file);
				}
			});

2、通过递归的方式删除目录下的子目录

/**
	 * 删除某个目录
	 * @param path 要删除的目录路径
	 * @return
	 */
	private boolean deleteDirs(String path){
		Logger.d("path = " + path);
		File file = new File(path);
		if (!file.exists()){
			return true;
		}
		if (file.isDirectory()){
			File[] childs = file.listFiles();
			if (null == childs){
				return false;
			}
			boolean result = true;
			for (File child : childs){
				result = result && deleteDirs(child.getAbsolutePath());
			}
			Logger.d("file1-------------------" + file.toString());
			try{
				boolean ret = file.delete();
				return result && ret;
			}catch(Exception e){
				e.printStackTrace();
				return false;
			}
			
		}else {
			Logger.d("file2-------------------" + file.toString());
			try {
				boolean ret = file.delete();
				return ret;
			} catch (Exception e) {
				e.printStackTrace();
				return false;
			}
			
		}
	}

3、当目录中内容比较多,占用很大空间的时候,需要考虑阻塞主线程的情况,所以要继承AsyncTask来执行删除的操作

/**
	 * 通过异步的方式来执行删除操作
	 * @author ytkj
	 *
	 */
	private class DeleteFileTask extends AsyncTask{
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			Logger.d();
			mTextView.setVisibility(View.VISIBLE);
//			mTextView.setText("正在删除" + DELETE_PART + DELETE_FILE + "....");
			mTextView.setText(mContext.getResources().getString(R.string.delete_running, DELETE_PART + DELETE_FILE, "...."));
		}
		
		@Override
		protected String doInBackground(String... params) {
			Logger.d();
			String delete_part = params[0];
			final String delete_file = params[1];
			File part = new File(delete_part);
			Logger.d("delete_part = " + delete_part + " ,delete_file = " + delete_file + " ,part = " + part);
			if (!part.exists() || (!part.isDirectory())){
				Logger.d("part = " + part);
				return mContext.getResources().getString(R.string.please_check, part);
			}
			//返回要删除的目录列表
			File[] childFiles = part.listFiles(new FileFilter() {
				
				@Override
				public boolean accept(File pathname) {
					String name = pathname.getName().toLowerCase();
					Logger.d("accept---------------name = " + name);
//					return name.contains(delete_file);
					return name.equals(delete_file);
				}
			});
			
			if (null == childFiles){
				Logger.d("null == childFiles");
				return mContext.getResources().getString(R.string.please_check, delete_part + delete_file);
			}else {
				boolean result = true;
				for (File child : childFiles){
					Logger.d("child = " + child.getAbsolutePath());
					boolean ret = deleteDirs(child.getAbsolutePath());
					result = result && ret;
					if (!ret){
						Logger.d("delete failed : " + child.getAbsolutePath());
					}
				}
				return result ? (mContext.getResources().getString(R.string.delete_success, delete_part + delete_file))
						: (mContext.getResources().getString(R.string.delete_fail, delete_part + delete_file));
			}
			
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			Logger.d("result = " + result);
			mTextView.setText(result);
		}
	}

4、点击onClick来测试删除目录的结果:

@Override
	public void onClick(View v) {
		Logger.d();
		String delete_part = deletePart.getText().toString().trim();
		String delete_file = deleteFile.getText().toString().trim();
		if (v.getId() == R.id.delete_file){
			if (!TextUtils.isEmpty(delete_part)){
				if (!TextUtils.isEmpty(delete_file)){
					new DeleteFileTask().execute(delete_part, delete_file);
				}
			}else{
				mTextView.setText( mContext.getResources().getString(R.string.please_check, delete_part));
			}
			
		}
		
	}

5、例如要删除/sdcard/LOG这个目录,布局如下:

Android 删除sdcard目录中的某些目录文件_第1张图片

6、执行删除操作后结果

Android 删除sdcard目录中的某些目录文件_第2张图片

日志打印:

Android 删除sdcard目录中的某些目录文件_第3张图片

Android 删除sdcard目录中的某些目录文件_第4张图片

代码参考:https://github.com/gunder1129/android-tool/tree/master/ToolTemplate

 

你可能感兴趣的:(android)