Android正确进行数据存储

 

数据存储要满足多用户存储要求,以及数据权限访问要求。禁止写死绝对路径,要些,存储选择:
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 弹跳

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值