Android 网络与数据存储
来源:互联网 发布:淘宝势力周多久一次 编辑:程序博客网 时间:2024/04/30 15:49
使用SharedPreferences 方便地存储数据
数据持久化
Android provides many ways of storing data of an application. One of this way is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key,value pair.
In order to use shared preferences , you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
提取一个方法ctrl + shift + m 或者refactor - extract - method
Editor\commit() -> apply(); //apply 后台写数据,另开线程;和网络,I/O相关,都要用异步
SharedPreferences 系统自动创建xml文件“preference_name” 数据存储在data/data/packageName/Shared_Pref
清除缓存:adb idea settings-plugins
如何随心所欲地管理文件
Internal storage
External storage
To access external storage:
android.permission.WRITE_EXTERNAL_STORAGE //declare in Manifestanroid:installlocation = "preferExternal"
在外部存储空间安装应用
Internal storage:
getFilesDir(); //返回一个file,代表了app的internal 目录getCacheDir(); //返回一个file,代表app的internal缓存目录
create a file and get its path,
File file = new File(getFilesDir(), "test.txt");Log.i("MainActivity","getFilesDir:" + getFilesDir().getAbsolutePath());Log.i("MainActivity","file path:" + file.getAbsolutePath());try { boolean isSuccess = file.createNewFile();} catch (IOException e){ e.printStackTrack();}
try-catch 在iMooc上有相关教学: http://www.imooc.com/learn/110
检查外部存储状态:
String state = Environment.getExternalStorageState();if(TextUtils.equals(state, Environment.MEDIA_MOUNTED)){}
assets
读取文件
void testAssets(){ WebView webView = new WebView(this); webView.loadUrl("file:///android_asset/test.html"); InputStream inputStream = getResources().getAssets().open("test.html");//获取资源, open 不可以用在文件夹只能对文件起作用}
读取assets里的文件
读取列表
String[] fileNames = getAssets().list("images/");
读取图片:
InputStream inputStream = getAssets().open("images/pic.jpg");Bitmap bitmap = BitmapFactory.decodeStream(inputStream); ImageView imageView = new ImageView(this);imageView.setImageBitmap(bitmap);
读取音乐文件
AssetFileDescriptor assetFileDescriptor = getAssets().openFd("music_title.mp3"); MediaPlayer = new MediaPlayer(); player.reset();player.setDataSource( assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength(); player.prepare();player.start(); )
read files under raw directory:
void testResFile(){ InputStream inputStream = getResources().openRawResource(R.raw.file);}
assets vs. raw vs. res
读取sd卡的文件
void testSDCard(){ File file = new File ("/sdcard/test/a.txt"); //not recommended, path name may vary String filePath = Environment.getExternalStorageDirectory()/getAbsolutePath(); Environment.getDataDirectory(); //获取Android中的data数据目录}
补充阅读
http://developer.android.com/reference/android/content/SharedPreferences.html
http://blog.csdn.net/wxyyxc1992/article/details/17222841
http://www.tutorialspoint.com/android/android_shared_preferences.htm
学习源码直接在AS里:
https://github.com/googlesamples
AS: resources: android-sdk-samples
AS: android-sdk-samples-android apiLevel - legacy-ApiDemos
AS: file-import projects - android-sdk…
- Android 网络与数据存储
- Android网络与数据存储01
- Android网络与数据存储02
- 网络与数据存储
- android 数据网络存储
- Android网络存储数据
- Android数据存储--网络存储
- Android数据存储网络存储数据
- android 数据存储 之 《网络存储数据》
- 第7讲 Android网络与数据存储
- Android Network网络数据存储
- Android网络与数据存储——网络编程数据处理(网络请求解析Json,解析xml)
- Android的数据存储方式------------网络存储数据
- Android网络与数据存储_学习笔记_第六周:SQLite与ContentProvider
- android数据存储与访问
- Android数据存储与访问
- Android数据存储与IO
- Android数据存储与IO
- 无序n个元素中,寻找第K大的元素 O(n)
- EF+Oracle+Unity+MVC+AutoMapper 框架的搭建
- Tsinsen A1116 幂方分解
- 树莓派一些常用设置
- Chromium网页渲染调度器(Scheduler)实现分析
- Android 网络与数据存储
- 搭建开发环境
- 心得
- Scala - Parentheses and Curly Brackets in Anonymous Functions
- Ubuntu安装攻略
- Hibernate4学习总结(3)--注解形式的基础映射,主键映射,基本属性映射,复合属性映射,继承映射。
- OC阅读笔记十一:引用计数
- DBL_EPSILON和 FLT_EPSILON的用法
- HDUOJ_1998_奇数阶魔方