知識社群登入
位置: 艾鍗學院 Blog > 專業論壇 > 討論
[Android實作問題] Android App 移除時該如何將SDCard檔案刪除
1樓
請有經驗的朋友幫幫忙~

Android App 移除時該如何將SDCard檔案刪除
目的是想要將App暫存於SDCard內的檔案在移除時一併刪除
1 避免下次App重新安裝時SQLite與檔案目錄不一致!
2 授權檔案在App移除後也一併刪除
2樓

請看我以下標註為紅色的部份,基本上你可以看一下你手機的SD Card目錄,會有一個
Android/data/packageName.... 的資料區,該區的資料來就是Android制定來給開發
者存放資料用,該區的資料自動隨著你的APP被移除時自動刪除,Google的這個方便的
設計是在LEVEL 8開始後提供,只是大部份的人都沒有注意到而以.

Accessing files on external storage

If you're using API Level 8 or greater, use getExternalFilesDir() to open a File that represents the external storage directory where you should save your files. This method takes a type parameter that specifies the type of subdirectory you want, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (pass null to receive the root of your application's file directory). This method will create the appropriate directory if necessary. By specifying the type of directory, you ensure that the Android's media scanner will properly categorize your files in the system (for example, ringtones are identified as ringtones and not music). If the user uninstalls your application, this directory and all its contents will be deleted.

If you're using API Level 7 or lower, use getExternalStorageDirectory(), to open a File representing the root of the external storage. You should then write your data in the following directory:

/Android/data/<package_name>/files/

The <package_name> is your Java-style package name, such as "com.example.android.app". If the user's device is running API Level 8 or greater and they uninstall your application, this directory and all its contents will be deleted.