数据存储要满足多用户存储要求,以及数据权限访问要求。禁止写死绝对路径,要些,存储选择:
Shared Preferences
Store private primitive data in key-value pairs.
Internal Storage
Store private data on the device memory.
External Storage
Store public data on the shared external storage.
SQLite Databases
Store structured data in a private database.
Network Connection
Store data on the web with your own network server.
(1)data目录
调用Context类的函数获取路径,操作文件,禁止在应用程序中写死绝对路径
getFilesDir()
Gets the absolute path to the filesystem directory where your internal files are saved.
getDir()
Creates (or opens an existing) directory within your internal storage space.
deleteFile()
Deletes a file saved on the internal storage.
fileList()
Returns an array of files currently saved by your application.
多用户提示:在主用户下,打印结果为:/data/data/com.yulong.android.coolmap切换到新建的第二个用户(对应userId==10)后,打印结果为:以酷派地图举例,/data/user/10/com.yulong.android.coolmap,依次,新建的其他用户获取data数据目录的打印结果为:/data/user/11/com.yulong.android.coolmap(对应userId==11),/data/user/12/com.yulong.android.coolmap(对应userId==12), /data/user/13/com.yulong.android.coolmap(对应userId==13)。
(2)SD卡存储目录:
格式Android/data/com.example.foo/,要调用mContext
Starting in Android 4.4, the owner, group and modes of files on external storage devices are now synthesized based on directory structure. This enables apps to manage their package-specific directories on external storage without requiring they hold the broad WRITE_EXTERNAL_STORAGE permission. For example, the app with package name com.example.foo can now freely access Android/data/com.example.foo/ on external storage devices with no permissions. These synthesized permissions are accomplished by wrapping raw storage devices in a FUSE daemon.
/** Multi-user external storage should be mounted. */
public static final int MOUNT_EXTERNAL_MULTIUSER = 2;
/** All multi-user external storage should be mounted. */
public static final int MOUNT_EXTERNAL_MULTIUSER_ALL = 3;
http://source.android.com/devices/tech/storage/index.html
http://source.android.com/devices/tech/storage/config.html
http://source.android.com/devices/tech/storage/config-example.html
Saving files that can be shared with other apps
Generally, new files that the user may acquire through your app should be saved to a "public" location on the device where other apps can access them and the user can easily copy them from the device. When doing so, you should use to one of the shared public directories, such as Music/, Pictures/, and Ringtones/.
To get a File representing the appropriate public directory, callgetExternalStoragePublicDirectory(), passing it the type of directory you want, such as DIRECTORY_MUSIC, DIRECTORY_PICTURES,DIRECTORY_RINGTONES, or others. By saving your files to the corresponding media-type directory, the system's media scanner can properly categorize your files in the system (for instance, ringtones appear in system settings as ringtones, not as music).
For example, here's a method that creates a directory for a new photo album in the public pictures directory:
Hiding your files from the Media Scanner
Include an empty file named .nomediain your external files directory (note the dot prefix in the filename). This prevents media scanner from reading your media files and providing them to other apps through the MediaStorecontent provider. However, if your files are truly private to your app, you shouldsave them in an app-private directory.
power by 弹跳