Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it programmer responsibility to delete temp file

Tags:

android

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?

like image 911
Cheok Yan Cheng Avatar asked Nov 04 '22 05:11

Cheok Yan Cheng


2 Answers

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.

like image 164
m0skit0 Avatar answered Nov 09 '22 11:11

m0skit0


Its upto each and every application to keep the house (or rather the device) clean. :)

That being said

  • The JVM is always running as far as android is booted up and running. So the file should get deleted if the device is restarted.
  • There is good chance that the user will get offended if an app is leaving temporary files even when the app has been closed.

So close the temporary file yourself, when you are done with it. Need not even be until the app exits!

like image 22
bluefalcon Avatar answered Nov 09 '22 12:11

bluefalcon