二、数据存储之File
在Android中通常使用File存储方式是用Context.openFileOutput(String fileName, int mode)和Context.openFileInput(String fileName)。
Context.openFileOutput(String fileName, int mode)生成的文件自动存储在/data/data/Package Name/files目录下,其全路径是/data/data/Package Name/files/fileName 。注意下,这里的参数fileName不可以包含路径分割符(如"/")。
通常来说,这种方式生成的文件只能在这个apk内访问。但这个结论是指使用Context.openFileInput(String fileName)的方式。使用这种方式,每个apk只可以访问自己的/data/data/Package Name/files目录下的文件,原因很简单,参数fileName中不可以包含路径分割符,Android会自动在/data/data/Package Name/files目录下寻找文件名为fileName的文件。
但是如果你直接使用这个File,那么这个File在其它apk中也是可以访问的,不过要注意在之前调用Context.openFileOutput(String file, int mode)时不要使用缺省的mode:MODE_PRIVATE ,而应该使用MODE_WORLD_READABLE 。使用缺省mode生成的文件的权限是“660”(也就是rw-rw----),而使用后者生成文件的权限是允许运行别的apk访问的。代码如下:
File file = new File("/data/data/Package Name/files/fileName");
另外还有一个方法可以改变这个生成文件的权限。可以直接在Java代码中执行Linux命令,毕竟Android归根到底也是Linux .代码如下:
Process process = Runtime.getProcess().exec("chmod 666 /data/data/Package Name/files/fileName");
process.waitFor();
下面是一个小例子:
public class ActivityMain1 extends Activity {
private TextView textview1=null;
private MIDIPlayer midiPlayer=null;
private boolean music=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化组件
textview1=(TextView)this.findViewById(R.id.texview1);
midiPlayer = new MIDIPlayer(this);
// //装载配置,获得SharePreferences对象
// SharedPreferences settings=this.getPreferences(MODE_PRIVATE);
// music=settings.getBoolean("music", false);
load();
if(music){
textview1.setText("当前音乐状态:开");
music=true;
midiPlayer.PlayerMusic(); //开始播放音乐
}else{
textview1.setText("当前音乐状态:关");
music=false;
midiPlayer.StopMusic(); //停止播放音乐
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_DPAD_DOWN:
textview1.setText("当前音乐状态:关");
music=false;
midiPlayer.StopMusic();
break;
case KeyEvent.KEYCODE_DPAD_UP:
textview1.setText("当前音乐状态:开");
music=true;
midiPlayer.PlayerMusic();
break;
}
return true;
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
SharedPreferences puts=this.getPreferences(0);
SharedPreferences.Editor eidit=puts.edit();
eidit.putBoolean("music", music);
eidit.commit();
save();
if(music){
midiPlayer.StopMusic();
}
this.finish();
return true;
}
return super.onKeyUp(keyCode, event);
}
//装载读取数据
public void load(){
//构建Properties对象
Properties properties = new Properties();
/*
* 打开文件,catch里面必须加上return;因为在第一次运行的时候,在创建的文件里什么没有,会抛出异常,
* 这时程序就会出错,所以把e.printStackTrace去掉,或加上return;程序就可以正常运行了。
*/
try {
FileInputStream stream=this.openFileInput("music.cfg");
//加载数据文件
properties.load(stream);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return;
//e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
return;
//e.printStackTrace();
}
//取得数据
music=Boolean.valueOf(properties.get("music").toString());
}
//保存数据
boolean save (){
Properties preoperties=new Properties();
//将数据打包成properties
preoperties.put("music", String.valueOf(music));
try {
FileOutputStream stream=this.openFileOutput("music.cfg", Context.MODE_WORLD_WRITEABLE);
preoperties.store(stream, "");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
return false;
}
return true;
}
}
这是MIDIPlayer.java文件
public class MIDIPlayer {
private MediaPlayer meidiaPlayer;
private Context context;
public MIDIPlayer(Context c){
this.context=c;
}
//开始播放音乐
public void PlayerMusic(){
//创建MediaPlayer播放器
meidiaPlayer=MediaPlayer.create(context, R.raw.start);
//是否循环
meidiaPlayer.setLooping(true);
//准备
try {
meidiaPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//开始
meidiaPlayer.start();
}
public void StopMusic(){
if(meidiaPlayer!=null){
meidiaPlayer.stop(); //停止播放
meidiaPlayer.release(); //释放meidiaPlayer
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/texview1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
在res文件下,新建一个raw文件夹,如图,
完成以上步骤后,运行就可以了。