Android 网络与数据存储

使用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 Manifest
anroid: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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值