使用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…

更多相关文章

  1. 【Android】基础1
  2. android(9)_数据存储与访问3_scard_login事例
  3. android service
  4. android操作sdcard
  5. Android带进度条文件上传
  6. Android的本地网络组件
  7. Android的NDK开发(3)————JNI数据类型的详解
  8. android两种方法操作Sqlite数据库
  9. Android(安卓)SMS(一) —— 读取短信

随机推荐

  1. Android(安卓)可下拉上拉效果的ScrollVie
  2. Android获取IMEI和ICCID
  3. Android实现各种对话框的变体
  4. Android(安卓)高德地图 定位+围栏
  5. Android联系人1--分组查询
  6. Android(安卓)ProgressBar 进度条颜色和
  7. android 混淆
  8. android调用系统分享
  9. Android进度条实例
  10. Android使用HttpURLConnection上传文件