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…

0 0
原创粉丝点击