Thursday, August 5, 2010

每一個開發者都應該要注意的事 (SD card 相關)

隨著 Android 越來越普及, 軟件數量幾何級上升, 試用新軟件已經成為一種樂趣.

但當經過一段時間, 用家不難發覺 SD card 的根目錄會變得十分混亂. 因為不同的開發者都有建立自己的文件夾, 當軟件被刪除後文件夾卻留在 SD card 裡. 原來由 Froyo 開始, 開發文件便有提及外存路徑的正確位置, 只要開發者把軟件的存取路徑置為 getExternalFilesDir() 或 " /Android/data//files/", 軟件被刪除的時候文件夾及裡面的內容會被一同刪除.

多一個軟件使用正確路徑, 就少一分混亂. 為保持一個良好 SD card 環境, 請各開發者將你下一個更新把軟件使用的文件夾放入正確位置.

注: 該路徑亦會於 Upgrade 軟件時刪除, 如有大量要保留下載資料的軟件可能不適用.

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: 
The is your Java-style package name, such as ""

No comments: