Currently, I have an application, which will save a temporary screenshot, and let user share it through facebook. The image saving code is more or less as follow.
// 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();
I realize even I quit my Android application (By pressing system "back" button), the tempFile is still there even we are using deleteOnExit
. My guess is that the JVM is not terminated yet.
May I know, is it our application programmer responsibility, to delete the created temp file, during Activity's onDestroy
? If not, what is the common best practice?
AFAIK yes, it's your responsibility to delete files not needed anymore (be they temporary or not). OS cannot know how long your app will be using that file.
Its upto each and every application to keep the house (or rather the device) clean. :)
That being said
So close the temporary file yourself, when you are done with it. Need not even be until the app exits!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With