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