开发者

How to create Application specific directory on SDCard that should be deleted when the app is uninstalled

I want to use the device's SD Card for storing my application files, images, cache files etc on the device and use them in my application. I know that I can save them on the internal memory by using Context.getFilesDir() / Context.openFileOutput methods but I do not want my application to use inter开发者_JAVA技巧nal memory for saving this data.

So is there any way I can create/save all my application specific files in a directory on the SD Card, but still be able to let the system know that these files have to be deleted when the application is uninstalled?

Or what are the recommendations in this case.

EDIT: I want to support 2.1 also.

Thank you.


Your application doesn't get notified when it is being deleted, so you might be better off using the application's private directory, (from getFilesDir()). That directory is automatically deleted when the app is uninstalled.

Keep in mind that if you are on SDK 2.2+, you can allow your users the option of moving the entire application to the SD card, so you won't have to worry about the size of this directory.


Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled. Link might help.


This is a bit of a hack but it works for me...

File myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/<package name>/files");
myFilesDir.mkdirs();

Replace '<package name>' with your package name.

EDIT: Thinking about it, it's possible it may only be 'cleaned up' on app un-installation on versions of Android from v2.2 onwards.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜