Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, delete files in my data directory?

My application stores some files that are required for it to run in the data directory of the app

/data/data/com.example.myapp/files/filehere.file

When my application is updated from the market, it is important that I clear the files in the data directory, and update them to the latest ones from the new package that has just been downloaded.

My question is, how do I programatically delete the files in the directory in question?

Alternatively, is there an alternative directory that I can store the files in that are persistent across the lifetime of my application version, but will be cleared once an update is installed?

like image 721
Hamid Avatar asked Apr 12 '11 15:04

Hamid


2 Answers

File file = new File(FILEPATH);
if(file.exists())
  file.delete();

As for your last question, my guess is you'll have to control that on your upgrade process. f.e., create a directory for each version and manage the files inside when the user upgrades.

like image 128
Maragues Avatar answered Sep 27 '22 18:09

Maragues


You might be better off using a database wrapped in SQLiteOpenHelper. You can then use the onUpgrade function to detect a new app version.

like image 31
Phil Lello Avatar answered Sep 27 '22 17:09

Phil Lello