SharedPreferences存储数据
1
2
3
4
5
|
SharedPreferences sharedPreferences =getSharedPreferences(
"lxt008"
, Context.MODE_PRIVATE);
Editor editor =sharedPreferences.edit();
//获取编辑器
editor.putString(
"name"
,
"lxt"
);
editor.putInt(
"age"
,
35
);
editor.commit();
|
生成的lxt008.xml文件内容如下:
1
2
3
4
5
|
<?xml version=“
1.0
” encoding=“utf-
8
” standalone=“yes” ?>
<map>
<string name=
"name"
>lxt</string>
<
int
name=
"age"
value=“
30
" />
</map>
|
getSharedPreferences(name,mode)方法
参数1:指定该文件名称,名称不用带后缀。
参数2:指定文件的操作模式,共有四种操作模式。
1
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用
2
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
3-1
Context.MODE_WORLD_READABLE和
Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
4-1
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;
4-2
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
getPreferences(mode)方法操作SharedPreferences,这个方法默认使用当前类不带包名的类名作为文件的名称。
android有一套自己的安全模型,当应用程序(.apk)在安装时系统就会分配给他一个userid,当该应用要去访问其他资源比如文件的时候,就需要userid匹配。
默认情况下,应用创建的文件/sharedpreferences/数据库都应该是私有的(位于/data/data/包名),其他程序无法访问。
除非在创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE ,只有这样其他程序才能正确访问。
访问SharedPreferences数据
访问SharedPreferences中的数据代码如下:
1
2
3
4
|
SharedPreferences sharedPreferences =getSharedPreferences(
"lxt008"
, Context.MODE_PRIVATE);
//getString()第二个参数为缺省值,如果preference中不存在该key,将返回缺省值
String name =sharedPreferences.getString(
"name"
,
""
);
int
age = sharedPreferences.getInt(
"age"
,
1
);
|
访问其他应用SharedPreferences数据
Android文件操作
7.3.1 文件存储
7.3.2读文件操作
7.3.3 写文件操作
7.3.4读取静态文件
访问其他应用Preference,前提条件是:该preference创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE。
getSharedPreferences(“lxt008", Context.MODE_WORLD_READABLE);
其他应用要访问上面应用的preference,首先需要创建上面应用的Context,然后通过Context访问preference,访问preference时会在应用所在包下的shared_prefs目录找到preference :
1
2
3
4
|
Context otherAppsContext =createPackageContext(“com.lxt008", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences =otherAppsContext.getSharedPreferences(“lxt008", Context.MODE_WORLD_READABLE);
String name =sharedPreferences.getString(
"name"
,
""
);
int
age =sharedPreferences.getInt(
"age"
,
0
);
|
7.3.1 文件存储
文件可以存储比使用引用更大数量的数据
Android 提供方法来读、写文件
只有本地文件可以被访问
优点:可以存储大容量的数据
缺点:文件更新或是格式改变可能会导致巨大的编程工作
7.3.2读文件操作
Context.openFileInput(String name)
打开一个与应用程序联系的私有文件输入流
当文件不存在时抛出FileNotFoundException
1
2
3
|
FileInputStream in =
this
.openFileInput(
"test.txt"
);
//打开文件test.txt
……
in.close();
//关闭输入流
|
Context.openFileOutput(String name,int mode)
开启一个与应用程序联系的私有文件输出流
当文件不存在时该文件将被创建
文件输出流可以在添加模式中打开,这意味新的数据将被添加到文件的末尾
1
2
3
|
FileOutputStream out=
this
.openFileOutput(
"test2.txt"
,MODE_APPEND);
//打开文件"test2.txt"进行写操作、使用MODE_APPEND 在添加模式中打开文件
……
out.close();
//关闭输出流
|
要打开打包在程序中的静态文件,使用Resources.openRawResource(R.raw.mydatafile)
该文件必须放在文件夹res/raw/中
1
2
3
|
InputStreamin =
this
.getResources().openRawResource(R.raw.my);
…
//获得Context资源
in.close();
//关闭输入流
|
文件操作案例
参考:FileMidiPlayer
7.3.5使用文件进行数据存储
首先给大家介绍使用文件如何对数据进行存储,Activity
提供了openFileOutput()
方法可以用于把数据输出到文件中,具体的实现过程与在J2SE
环境中保存数据到文件中是一样的。
1
2
3
4
5
6
7
8
|
public
class
FileActivity
extends
Activity{
@Override
public
void
onCreate(Bundle savedInstanceState) {
...
FileOutputStream outStream =
this
.openFileOutput(“lxt008.txt",Context.MODE_PRIVATE);
outStream.write(“lxt008".getBytes());
outStream.close();
}
}
|
openFileOutput()
方法的第一参数用于指定文件名称,不能包含路径分隔符“/”
,如果文件不存在,Android
会自动创建它。创建的文件保存在/data/data//files
目录,如:/data/data/com.lxt008/files/lxt008.txt
,通过点击Eclipse
菜单“Window”-“Show View”-“Other”
,在对话窗口中展开android
文件夹,选择下面的File Explorer
视图,然后在File Explorer
视图中展开/data/data//files
目录就可以看到该文件。
openFileOutput()
方法的第二参数用于指定操作模式,有四种模式,分别为:
Context.MODE_PRIVATE = 0
Context.MODE_APPEND = 32768
Context.MODE_WORLD_READABLE = 1
Context.MODE_WORLD_WRITEABLE = 2
读取文件内容
如果要打开存放在/data/data//files
目录应用私有的文件,可以使用Activity
提供openFileInput()
方法。
1
2
|
FileInputStream inStream =
this
.getContext().openFileInput(“lxt008.txt");
Log.i(
"FileTest"
,readInStream(inStream));
|
readInStream()
的方法请看本页下面备注。
或者直接使用文件的绝对路径:
1
2
3
|
File file = newFile(
"/data/data/com.lxt008/files/lxt008.txt"
);
FileInputStream inStream =
new
FileInputStream(file);
Log.i(
"FileTest"
,readInStream(inStream));
|
注意:上面文件路径中的“com.lxt008”
为应用所在包,应替换为你自己应用使用的包。
对于私有文件只能被创建该文件的应用访问,如果希望文件能被其他应用读和写,可以在创建文件时,指定Context.MODE_WORLD_READABLE
和Context.MODE_WORLD_WRITEABLE
权限。
Activity
还提供了getCacheDir()
和getFilesDir()
方法:
getCacheDir()
方法用于获取/data/data//cache
目录
getFilesDir()
方法用于获取/data/data//files
目录
SDCard文件存取7.4.1 把文件存放在SDCard
Environment.getExternalStorageState()方法用于获取SDCard的状态,如果手机装有SDCard,并且可以进行读写,那么方法返回的状态等于Environment.MEDIA_MOUNTED。
Environment.getExternalStorageDirectory()方法用于获取SDCard的目录,当然要获取SDCard的目录,你也可以这样写:
使用Activity
的openFileOutput()
方法保存文件,文件是存放在手机空间上,一般手机的存储空间不是很大,存放些小文件还行,如果要存放像视频这样的大文件,是不可行的。对于像视频这样的大文件,我们可以把它存放在SDCard
。 SDCard
是干什么的?你可以把它看作是移动硬盘或U
盘。
在模拟器中使用SDCard
,你需要先创建一张SDCard
卡(当然不是真的SDCard
,只是镜像文件)。创建SDCard
可以在Eclipse
创建模拟器时随同创建,也可以使用DOS
命令进行创建,如下:
在Dos
窗口中进入android SDK
安装路径的tools
目录,输入以下命令创建一张容量为2G
的SDCard
,文件后缀可以随便取,建议使用.img
:
mksdcard 2048M D:\AndroidTool\sdcard.img
在程序中访问SDCard,你需要申请访问SDCard的权限。
在AndroidManifest.xml
中加入访问SDCard
的权限如下:
1
2
3
4
|
<!-- 在SDCard中创建与删除文件权限 -->
<
uses-permission
android:name
=
"android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
/>
<!-- 往SDCard写入数据权限 -->
<
uses-permission
android:name
=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
|
要往SDCard
存放文件,程序必须先判断手机是否装有SDCard
,并且可以进行读写。
注意:访问SDCard
必须在AndroidManifest.xml
中加入访问SDCard
的权限
1
2
3
4
5
6
7
|
if
(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File sdCardDir = Environment.getExternalStorageDirectory();
//获取SDCard目录
File saveFile =
new
File(sdCardDir, “lxt008.txt”);
FileOutputStream outStream =
new
FileOutputStream(saveFile);
outStream.write(“lxt008".getBytes());
outStream.close();
}
|
Environment.getExternalStorageState()方法用于获取SDCard的状态,如果手机装有SDCard,并且可以进行读写,那么方法返回的状态等于Environment.MEDIA_MOUNTED。
Environment.getExternalStorageDirectory()方法用于获取SDCard的目录,当然要获取SDCard的目录,你也可以这样写:
1
2
|
File sdCardDir =
new
File(
"/sdcard"
);
//获取SDCard目录
File saveFile =
new
File(sdCardDir, “lxt008.txt");
|
//
上面两句代码可以合成一句:
1
2
3
4
|
File saveFile =
new
File(
"/sdcard/lxt008.txt"
);
FileOutputStream outStream =
new
FileOutputStream(saveFile);
outStream.write(“lxt008".getBytes());
outStream.close();
|