锘??xml version="1.0" encoding="utf-8" standalone="yes"?> To get a To write values: To read values, use Here is an example that saves a preference for silent keypress mode in a calculator:
2
3 Bitmap pngBM = BitmapFactory.decodeStream(picUrl.openStream());
4
5 imageView.setImageBitmap(pngBM);
寰堢畝鍗曞惂錛屽摢浣嶆湅鍙嬪鏋滄湁鏇寸畝鍗曠殑鏂規(guī)硶璇烽氱煡鎴戙?br />
]]>
The SharedPreferences
class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences
to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
SharedPreferences
object for your application, use one of two methods:
getSharedPreferences()
- Use this if you need multiple preferences files identified by name, which you specify with the first parameter.getPreferences()
- Use this if you need only one preferences file for your Activity. Because this will be the only preferences file for your Activity, you don't supply a name.
edit()
to get a SharedPreferences.Editor
.putBoolean()
and putString()
.commit()
SharedPreferences
methods such as getBoolean()
and getString()
.
public class Calc extends Activity {
2 public static final String PREFS_NAME = "MyPrefsFile";
3
4 @Override
5 protected void onCreate(Bundle state){
6 super.onCreate(state);
7 . . .
8
9 // Restore preferences
10 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
11 boolean silent = settings.getBoolean("silentMode", false);
12 setSilent(silent);
13 }
14
15 @Override
16 protected void onStop(){
17 super.onStop();
18
19 // We need an Editor object to make preference changes.
20 // All objects are from android.context.Context
21 SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
22 SharedPreferences.Editor editor = settings.edit();
23 editor.putBoolean("silentMode", mSilentMode);
24
25 // Commit the edits!
26 editor.commit();
27 }
28}
]]>
涓銆?nbsp;files
1. Context.getFilesDir()錛岃鏂規(guī)硶榪斿洖/data/data/youPackageName/files鐨凢ile瀵硅薄銆?br />
2. Context.openFileInput()涓嶤ontext.openFileOutput()錛屽彧鑳借鍙栧拰鍐欏叆files涓嬬殑鏂囦歡錛岃繑鍥炵殑鏄疐ileInputStream鍜孎ileOutputStream瀵硅薄銆?br />
3. Context.fileList()錛岃繑鍥瀎iles涓嬫墍鏈夌殑鏂囦歡鍚嶏紝榪斿洖鐨勬槸String[]瀵硅薄銆?br />
4. Context.deleteFile(String)錛屽垹闄iles涓嬫寚瀹氬悕縐扮殑鏂囦歡銆?br />
浜屻乧ache
1. Context.getCacheDir()錛岃鏂規(guī)硶榪斿洖/data/data/youPackageName/cache鐨凢ile瀵硅薄銆?br />
涓夈乧ustom dir
2.閫傜敤浜嶩ero錛堟湭嫻嬭瘯鏄惁鏀寔鍏朵粬HTC鎵嬫満錛?/span>
3.鐩墠涓烘鍦ㄥ鏁版墜鏈轟笂宸ヤ綔姝e父鐨勪唬鐮?/span>
闇瑕佹敞鎰忕殑涓涓棶棰樻槸涔嬪墠涓鐩寸敤Uri鎸囧悜鑷繁寮鍙戝簲鐢ㄧ殑data鏂囦歡澶逛笅鐨勬枃浠訛紝濮嬬粓鏃犳硶鎴愬姛錛屾渶緇堟崲鎴恠dcard灝監(jiān)K浜嗭紝鐪嬫潵榪樻槸鏉冮檺鐨勯棶棰樸?/span>