Android开发笔记六
SharedPrefeence学习
1、数据持久化
2、把数据存起来,需要时在取出来
3、保存一些比较简单的数据
4、快捷键:fori可以直接创建迭代。
5、怎么用sharedprefence存储数据
创建sharedprefence类



保存数据时最好用editor.apply();后台运行,另开线程。
读取保存的数据

删除

6、字符串转化成整型

7、内部存储外部存储
获取存储权限android.permission.WRITE_EXTERNAL_STORAGE
android:installLocation(存储位置)
8、Internal storage
getFilesDir():返回一个File,代表了我们app的internal目录
getCacheDir():返回一个File,代表了我们的app的internal缓存目录
File file = new File(getFilesDir(),"test.txt");//在内存中创建一个文件(并没有真正的创建)
getFilesDir().getAbsolutePath();//文件的全路径
file.createNewFile();//创建文件
9、向文件写数据
String str= “对不对”;
FileOutputStream out = openFlieOutput("test2.txt",Context.MODE_PRIVATE);
out.write(str.getByted());
out.close()
10、写持久化数据
检查SD卡是否可用
String state = Enviroment.getExternalStorageState();
if(TextUtils.equals(state,Enviroment.MDIA_MOUNTED)){
}
11、文件的基础操作
12、读取各个目录的文件
SD卡、assets目录、raw目录、res目录
13、WebView加载html文件
//直接读路径
Webview加载assets中的文件:webview.loadUrl("file:///amdroid_aseet/test.html");
//读取文件流(不能是文件夹)
InputStream in = getResourses().getAssets().open(“test.html”);
//读取文件夹里的图片
InputStream in =getAssets().open(“images/dog.jpg”);
Bitmap bitmap = BiatmapFactory.decodeSream(in);
ImageView IN= new ImageView(this);
IN.setImageBitmap(bitmap)

//读列表
String[] filesname = getAssets.list("image")路径
//读音频
AssetFileDescriptor ass = getAssets().openFd("libai.mp3");
MediaPlay player = new MediaPlayer();
player.resset();
player.setDataSource(ass.getFileDescriptor(),ass.getStartOffset(),ass.getLength));
player.prepare();
player.start();
14、读取raw文件--res \raw不能再有目录
InputStream in = getResourses().getRawResource(R.raw.libai);
15 SD卡
(1)可以直接找路径
(2)File file = Enviroment.getExternakStorageDirectory().getAbsolutePath();//SD卡路径
(3)Enviroment.getDataDirectory();//获取data目录