您的位置:首页 > 移动开发 > Android开发

作业——在线学习Android课程之第五周

2016-04-03 22:48 441 查看

第一节 使用‘SharedPreferences’方便地存储数据

1、什么是SharedPreferences?

SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置,比如Activity状态,Activity暂停时,将此activity的状态保存到SharedPereferences中;当Activity重载,系统回调方法onSaveInstanceState时,再从SharedPreferences中将值取出。

SharedPreferences提供了java常规的Long、Int、String等类型数据的保存接口。

SharedPreferences类似过去Windows系统上的ini配置文件,但是它分为多种权限,可以全局共享访问。

提示最终是以xml方式来保存,整体效率来看不是特别的高,对于常规的轻量级而言比SQLite要好不少,如果真的存储量不大可以考虑自己定义文件格式。xml处理时Dalvik会通过自带底层的本地XML Parser解析,比如XMLpull方式,这样对于内存资源占用比较好。


2、SharedPreferences数据的四种操作模式

Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件

Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入


3、用法举例

其中,"setting"是xml文件名,"first_run"是变量名(关键字),true是默认值(在取不到对应值时生效)

//从setting.xml文件读取数据
private boolean readData4Preference(){
boolean bool;
SharedPreferences sharedPreferences = getSharedPreferences("setting", Context.MODE_PRIVATE);
bool = sharedPreferences.getBoolean("first_run", true);
return bool;
}

//保存数据至setting.xml文件
private void saveData2Preference(boolean bool){
SharedPreferences sharedPreferences = getSharedPreferences("setting", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putBoolean("first_run", bool);
editor.apply();
}


第二节 如何随心所欲地管理文件

Android的存储分内部存储和外部存储:
内部存储指系统所在盘;
外部存储一般指插到电脑上可看见的盘。

注意:使用外部存储需声明读写文件权限:
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
installLocation=""决定程序装在内部还是外部

getFilesDir(): 返回一个File,代表了我们app的internal目录。
getCacheDir(): 返回一个File,代表了我们app的internal缓存目录。

//创建一个文件叫text.txt,在internal目录
private void testFileDemo(){
File file = new File(getFilesDir(),"text.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

//再在文件中写数据
try {
FileOutputStream fileOutputStream  = openFileOutput("text.txt", Context.MODE_PRIVATE);
try {
fileOutputStream.write(s.getBytes());
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

//操作sd卡目录下的文件
void testSDcard(){
File file = new File("/sdcard/text/a.txt");
String tempFile = Environment.getExternalStorageDirectory().getAbsolutePath();
Environment.getDataDirectory();//获取android的data目录
Environment.getDownloadCacheDirectory();//获取下载的缓存目录
}

//操作assets目录下的文件
void testAssets{
//保存文件
WebView webView = new WebView(this);
webView.loadUrl("file://android_asset/test.html");
try {
//open的只能是文件不能是文件夾
getResources().getAssets().open("test.html");//讀取文件
} catch (IOException e) {
e.printStackTrace();
}
//读列表
try {
String[] fileNames = getAssets().list("images");
} catch (IOException e) {
e.printStackTrace();
}

InputStream inputStream = null;
try {
// 读图片
inputStream = getAssets().open("images/dog.jpg");

Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmap);
//读音频
AssetFileDescriptor assetFileDescriptor = getAssets().openFd("libai.mp3");
MediaPlayer player = new MediaPlayer();
player.reset();
player.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
}

//操作res目录下的文件
void testResFile(){
InputStream inputStream = getResources().openRawResource(R.raw.libai);
getResources().getColor(R.color.colorAccent);
}

//操作raw目录下的文件
void testResFile(){
InputStream inputStream = getResources().openRawResource(R.raw.libai);
getResources().getColor(R.color.colorAccent);
}

asset和raw都不会被编译,但raw会生成索引表,可通过R.XXX.XXX找到。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: