How to delete all temp files which created by createTempFile when exit an App in android?

前端 未结 7 2348
清酒与你
清酒与你 2021-02-07 15:06

I use the following code to create some temp files, and wrapped tem as inputsteam to send to client side.

I understand that the temp files can be deleted automatically b

7条回答
  •  旧巷少年郎
    2021-02-07 15:22

    Delete the files in onDestroy if isChangingConfigurations() is false or isFinishing is true. Example:

    @Override protected void onDestroy() {
      super.onDestroy();
      if(!isChangingConfigurations()) {
        deleteTempFiles(getCacheDir());
      }
    }
    
    private boolean deleteTempFiles(File file) {
      if (file.isDirectory()) {
        File[] files = file.listFiles();
        if (files != null) {
          for (File f : files) {
            if (f.isDirectory()) {
              deleteTempFiles(f);
            } else {
              f.delete();
            }
          }
        }
      }
      return file.delete();
    }
    

提交回复
热议问题