【发布时间】:2012-02-23 08:10:27
【问题描述】:
目前,我有一个应用程序,它将保存一个临时屏幕截图,并让用户通过 facebook 分享它。图片保存代码大致如下。
// Use external cache dir, as our image file is large (>1M)
File outputDir = this.getExternalCacheDir();
File tempFile = File.createTempFile("CHEOK", "CHEOK", outputDir);
// In Desktop's J2SE, this file will automatically be deleted, once my desktop
// application quit. However, this is not the case for Android. My guess is, JVM
// is not terminated, even our Android application had quit.
tempFile.deleteOnExit();
我意识到即使我退出了我的 Android 应用程序(通过按系统“返回”按钮),即使我们使用 deleteOnExit,tempFile 仍然存在。我的猜测是 JVM 还没有终止。
请问,在 Activity 的onDestroy 期间删除创建的临时文件是我们的应用程序程序员的责任吗?如果不是,常见的最佳做法是什么?
【问题讨论】:
-
我认为它没有被触发的原因是Android管理内存的方式,后退按钮不会真正杀死应用程序,而是将其留在内存中但只是不活动。我的 android 手机 SD 卡空间总是变小,因为很多应用程序不会删除任何缓存/临时文件,不知道为什么,但我认为最好在不需要它们时删除它们
标签: android