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

前端 未结 7 2334
清酒与你
清酒与你 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:19

    Try Using WorkManager to make sure it cleans up the temp files even after the app is closed.

        override fun doWork(): Result {
    
                return try {
                    applicationContext.cacheDir?.let {
                        if(it.exists()){
                            val entries = it.listFiles()
                            if (entries != null){
                                for (entry in entries) {
                                    Timber.i("My file: ${entry.name}")
                                    entry.delete()
                                }
                            }
                        }
                    }
                    Result.success()
                } catch (e: Exception) {
                    Timber.e(e)
                    Result.failure()
                }
    
            }
    

提交回复
热议问题