Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android應用開發教程之八:四種本地數據儲存方案全解析

編輯:關於 Android 開發

在 Android 應用開發中,數據的持久化儲存是每一個程式設計師都必須掌握的核心技能。本文將由淺入深地介紹四種常見的本地數據處理方式,分別是 SharedPreferences、自訂檔案、res/raw 唯讀資源以及 SD 卡,並附上完整的程式碼示範,幫助你靈活應對不同場景下的數據儲存需求。

一、使用 SharedPreferences 處理數據

SharedPreferences 會將資料以 XML 檔案的形式保存,內容以節點的方式組織在檔案中。它為開發者提供了一系列便捷的 API,可以輕鬆完成資料的新建、儲存、讀取與刪除。

首先在介面中輸入需要保存的內容,這裡我們以姓名與號碼為例:

輸入姓名:雨松MOMO

輸入號碼:15810463139

點擊儲存按鈕後,系統會提示保存成功。

保存成功後,資料會被寫入到 data 路徑下的「當前包名/shared_prefs/main.xml」檔案中。用編輯器打開這個檔案,可以清楚看到內容是以一個個節點的形式存儲在 XML 裡的。

SharedPreferences 提供了非常方便的方法來讀寫資料。需要特別說明的是,一個應用中可以同時存在多個 SharedPreferences 檔案,程式只需根據不同的 XML 名稱,就能透過對應的方法取得 respective 物件。由於它並不支援高效的批次遍歷查詢,因此在開發實務中,我們通常用它來存放一些零散、量小的資料,例如使用者設定的音量大小、上次輸入的搜尋關鍵字等等。

Java 程式碼:

  1. public class SPActivity extends Activity {  
  2.    
  3.     /**使用SharedPreferences 來儲存與讀取數據**/  
  4.     SharedPreferences mShared = null;  
  5.    
  6.     /**程序中可以同時存在多個SharedPreferences數據, 根據SharedPreferences的名稱就可以拿到對象**/  
  7.     public final static String SHARED_MAIN = "main";  
  8.    
  9.     /**SharedPreferences中儲存數據的Key名稱**/  
  10.     public final static String KEY_NAME = "name";  
  11.     public final static String KEY_NUMBER = "number";  
  12.    
  13.     /**SharedPreferences中儲存數據的路徑**/  
  14.     public final static String DATA_URL = "/data/data/";  
  15.     public final static String SHARED_MAIN_XML = "main.xml";  
  16.    
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.     setContentView(R.layout.sharedpreferences);  
  20.     /**拿到名稱是SHARED_MAIN 的SharedPreferences對象**/  
  21.     mShared = getSharedPreferences(SHARED_MAIN, Context.MODE_PRIVATE);  
  22.     /**拿到SharedPreferences中保存的數值 第二個參數為如果SharedPreferences中沒有保存就賦一個默認值**/  
  23.     String name = mShared.getString(KEY_NAME, "數據庫中沒有儲存姓名");  
  24.     String number = mShared.getString(KEY_NUMBER, "數據庫中沒有儲存號碼");  
  25.    
  26.     final EditText editName = (EditText)findViewById(R.id.sp_et0);  
  27.     final EditText editNumber = (EditText)findViewById(R.id.sp_et1);  
  28.     editName.setHint("上次輸入的姓名為【 " +name+"】");  
  29.     editNumber.setHint("上次輸入的號碼為【 " +number+"】");  
  30.    
  31.     Button button0 = (Button)findViewById(R.id.sp_button0);  
  32.    
  33.     /**監聽按鈕點擊後保存用戶輸入信息到SharedPreferences中**/  
  34.     button0.setOnClickListener(new  OnClickListener() {  
  35.    
  36.         @Override  
  37.         public void onClick(View arg0) {  
  38.         /**拿到用戶輸入的信息**/  
  39.         String name = editName.getText().toString();  
  40.         String number = editNumber.getText().toString();  
  41.         /**開始保存入SharedPreferences**/  
  42.         Editor editor = mShared.edit();  
  43.         editor.putString(KEY_NAME, name);  
  44.         editor.putString(KEY_NUMBER, number);  
  45.         /**put完畢必需要commit()否則無法保存**/  
  46.         editor.commit();  
  47.         ShowDialog("保存SharedPreferences成功");  
  48.    
  49.         }  
  50.     });  
  51.    
  52.     Button button1 = (Button)findViewById(R.id.sp_button1);  
  53.     button1.setOnClickListener(new  OnClickListener() {  
  54.    
  55.         @Override  
  56.         public void onClick(View arg0) {  
  57.         /**開始清除SharedPreferences中保存的內容**/  
  58.         Editor editor = mShared.edit();  
  59.         editor.remove(KEY_NAME);  
  60.         editor.remove(KEY_NUMBER);  
  61.         //editor.clear();  
  62.         editor.commit();  
  63.         ShowDialog("清除SharedPreferences數據成功");  
  64.         }  
  65.     });  
  66.    
  67.     Button button2 = (Button)findViewById(R.id.sp_button2);  
  68.     button2.setOnClickListener(new  OnClickListener() {  
  69.    
  70.         @Override  
  71.         public void onClick(View arg0) {  
  72.         /** 刪除SharedPreferences文件 **/  
  73.         File file = new File(DATA_URL + getPackageName().toString()  
  74.             + "/shared_prefs", SHARED_MAIN_XML);  
  75.         if (file.exists()) {  
  76.             file.delete();  
  77.         }  
  78.         ShowDialog("刪除SharedPreferences文件成功");  
  79.         }  
  80.     });  
  81.    
  82.     super.onCreate(savedInstanceState);  
  83.     }  
  84.    
  85.     public void ShowDialog(String string) {  
  86.     AlertDialog.Builder builder = new AlertDialog.Builder(SPActivity.this);  
  87.     builder.setIcon(R.drawable.icon);  
  88.     builder.setTitle(string);  
  89.     builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
  90.         public void onClick(DialogInterface dialog, int whichButton) {  
  91.         finish();  
  92.         }  
  93.     });  
  94.     builder.show();  
  95.     }  
  96. }  

對應的 XML 佈局如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.    
  3. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  4.    android:layout_width="fill_parent"  
  5.    android:layout_height="fill_parent"  
  6.    android:orientation="vertical"  
  7.    >  
  8.     <ImageView android:id="@+id/sp_image"  
  9.         android:layout_width="wrap_content"  
  10.          android:layout_height="wrap_content"  
  11.          android:src="@drawable/image"  
  12.         android:layout_gravity="center"  
  13.         />  
  14.     <EditText android:id="@+id/sp_et0"  
  15.               android:layout_width="fill_parent"  
  16.               android:layout_height="wrap_content"  
  17.               android:hint="請輸入你的姓名">  
  18.     </EditText>  
  19.     <EditText android:id="@+id/sp_et1"  
  20.               android:layout_width="fill_parent"  
  21.               android:layout_height="wrap_content"  
  22.               android:hint="請輸入你的號碼">  
  23.     </EditText>  
  24.     <Button   android:id="@+id/sp_button0"  
  25.         android:layout_width="wrap_content"  
  26.               android:layout_height="wrap_content"  
  27.               android:text="保存輸入內容shared">  
  28.     </Button>  
  29.     <Button   android:id="@+id/sp_button1"  
  30.         android:layout_width="wrap_content"  
  31.               android:layout_height="wrap_content"  
  32.               android:text="清除shared保存內容">  
  33.     </Button>  
  34.     <Button   android:id="@+id/sp_button2"  
  35.         android:layout_width="wrap_content"  
  36.               android:layout_height="wrap_content"  
  37.               android:text="刪除shared文件">  
  38.     </Button>  
  39. </LinearLayout>  

二、在 data 目錄下以自訂檔案處理數據

如果不希望把資料存在 SharedPreferences 中,我們也可以自行建立一個檔案來保存所需的資料。這裡選擇將檔案存放在系統的工程路徑下。

先輸入需要保存的內容:

保存完成後,紅框內會顯示上一次保存的資料:

保存之後,檔案會被存放到當前工程下 files 資料夾的路徑中。這裡要特別提醒:data 資料夾在手機未取得 root 權限時,一般使用者是無法直接存取的。這種方式有一個較為麻煩的地方——檔案中的資料需要由程式設計師自行解析處理。舉例來說,若檔案中保存了大量字串,而你只需要其中一部分,就必須自己撰寫程式碼,從檔案中擷取所需的內容。

Java 程式碼:

  1. public class FileActivity extends Activity {  
  2.     public final static String FILE_NAME = "a.txt";  
  3.    
  4.     /**File中儲存數據的路徑**/  
  5.     public final static String DATA_URL = "/data/data/";  
  6.    @Override  
  7.    protected void onCreate(Bundle savedInstanceState) {  
  8.    setContentView(R.layout.file);  
  9.    /**讀取內容**/  
  10.    String content = loadFile();  
  11.    if(content == null) {  
  12.        content = "上次沒有輸入內容請輸入";  
  13.    }  
  14.    String str  = "上次輸入保存的內容的姓名為【 " +content + "】";  
  15.    final EditText editContent = ((EditText)findViewById(R.id.file_et0));  
  16.    editContent.setHint(str);  
  17.    Button button0 = (Button)findViewById(R.id.file_button0);  
  18.    
  19.    /**監聽按鈕點擊後保存用戶輸入信息到file中**/  
  20.    button0.setOnClickListener(new  OnClickListener() {  
  21.        @Override  
  22.        public void onClick(View arg0) {  
  23.        /**拿到用戶輸入的信息**/  
  24.        String content = editContent.getText().toString();  
  25.        /**開始保存入file**/  
  26.        saveFile(content);  
  27.        ShowDialog("保存File文件成功");  
  28.        }  
  29.    });  
  30.    
  31.    Button button1 = (Button)findViewById(R.id.file_button1);  
  32.    /**監聽按鈕點擊後清空file中內容**/  
  33.    button1.setOnClickListener(new  OnClickListener() {  
  34.        @Override  
  35.        public void onClick(View arg0) {  
  36.        cleanFile();  
  37.        ShowDialog("清空File文件成功");  
  38.        }  
  39.    });  
  40.    
  41.    Button button2 = (Button)findViewById(R.id.file_button2);  
  42.    
  43.    /**監聽按鈕點擊後刪除file文件**/  
  44.    button2.setOnClickListener(new  OnClickListener() {  
  45.        @Override  
  46.        public void onClick(View arg0) {  
  47.        File file = new File(DATA_URL + getPackageName().toString()  
  48.             + "/files", FILE_NAME);  
  49.        if (file.exists()) {  
  50.             file.delete();  
  51.        }  
  52.        ShowDialog("刪除file文件成功");  
  53.        }  
  54.    });  
  55.    
  56.    super.onCreate(savedInstanceState);  
  57.    }  
  58.    
  59.    /** 
  60.     * 保存內容 
  61.     * @param str 
  62.     */  
  63.    public void saveFile(String str) {  
  64.    try {  
  65.        FileOutputStream outStream = this.openFileOutput(FILE_NAME,  
  66.             Context.MODE_WORLD_READABLE);  
  67.        outStream.write(str.getBytes());  
  68.        outStream.close();  
  69.    } catch (FileNotFoundException e) {  
  70.    } catch (IOException e) {  
  71.    }  
  72.    }  
  73.    
  74.    /** 
  75.     * 因為java刪除文件內容只有一種實現方法,就是把整個文件重寫,只是把須要刪除的那一條記錄去除掉 
  76.     */  
  77.    public void cleanFile() {  
  78.    //如果只須要刪除文件中的一部分內容則須要在這裡對字符串做一些操作  
  79.    String cleanStr = "";  
  80.    try {  
  81.        FileOutputStream outStream = this.openFileOutput(FILE_NAME,  
  82.             Context.MODE_WORLD_READABLE);  
  83.        outStream.write(cleanStr.getBytes());  
  84.        outStream.close();  
  85.    } catch (FileNotFoundException e) {  
  86.    } catch (IOException e) {  
  87.    }  
  88.    
  89.    }  
  90.    
  91.    public String loadFile() {  
  92.    try {  
  93.        FileInputStream inStream = this.openFileInput(FILE_NAME);  
  94.        ByteArrayOutputStream stream = new ByteArrayOutputStream();  
  95.        byte[] buffer = new byte[1024];  
  96.        int length = -1;  
  97.        while ((length = inStream.read(buffer)) != -1) {  
  98.        stream.write(buffer, 0, length);  
  99.        }  
  100.        stream.close();  
  101.        inStream.close();  
  102.        return stream.toString();  
  103.    } catch (FileNotFoundException e) {  
  104.        e.printStackTrace();  
  105.    } catch (IOException e) {  
  106.    }  
  107.    return null;  
  108.    }  
  109.    
  110.    public void ShowDialog(String str) {  
  111.    AlertDialog.Builder builder = new AlertDialog.Builder(FileActivity.this);  
  112.    builder.setIcon(R.drawable.icon);  
  113.    builder.setTitle(str);  
  114.    builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
  115.        public void onClick(DialogInterface dialog, int whichButton) {  
  116.        finish();  
  117.        }  
  118.    });  
  119.    builder.show();  
  120.    }  
  121. }  

XML 佈局:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.    
  3. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  4.    android:layout_width="fill_parent"  
  5.    android:layout_height="fill_parent"  
  6.    android:orientation="vertical"  
  7.    >  
  8.     <ImageView android:id="@+id/file_image"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:src="@drawable/jay"  
  12.         android:layout_gravity="center"  
  13.         />  
  14.    
  15.     <EditText android:id="@+id/file_et0"  
  16.               android:layout_width="fill_parent"  
  17.               android:layout_height="wrap_content"  
  18.               android:hint="請輸入需要保存的內容">  
  19.     </EditText>  
  20.     <Button   android:id="@+id/file_button0"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="保存入file">  
  24.     </Button>  
  25.     <Button   android:id="@+id/file_button1"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="清除file保存內容">  
  29.     </Button>  
  30.     <Button   android:id="@+id/file_button2"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="wrap_content"  
  33.         android:text="刪除file文件">  
  34.     </Button>  
  35. </LinearLayout>  

三、在 res/raw 目錄中讀取數據

Android 提供了專門讀取 res/raw 路徑下資源的方法,但並未提供寫入的介面,也就是說 raw 中的內容只能讀、不能寫。在實際開發中,我們有時需要讀取大量的文字資源,而這些文字在應用運行期間不會發生改變,無需重新寫入或修改,因此可以透過 raw 來操作這類數據。

下圖展示了在清單中讀取 .bin 檔案內容並逐行顯示於 listView 的效果:

如下圖所示,raw 路徑下存放了一個名為 date0.bin 的檔案,下面我們需要讀取該 .bin 檔案的內容並顯示在螢幕上。

以下是程式碼實現:

  1. public class loadRawActivity extends ListActivity {  
  2.    
  3.     private class MyListAdapter extends BaseAdapter {  
  4.     private int[] colors = new int[] { 0xff626569, 0xff4f5257 };  
  5.    
  6.     public MyListAdapter(Context context) {  
  7.         mContext = context;  
  8.     }  
  9.    
  10.     public int getCount() {  
  11.         return inpormation.length;  
  12.     }  
  13.    
  14.     @Override  
  15.     public boolean areAllItemsEnabled() {  
  16.         return false;  
  17.     }  
  18.    
  19.     public Object getItem(int position) {  
  20.         return position;  
  21.     }  
  22.    
  23.     public long getItemId(int position) {  
  24.         return position;  
  25.     }  
  26.    
  27.     public View getView(int position, View convertView, ViewGroup parent) {  
  28.         TextView tv;  
  29.         if (convertView == null) {  
  30.             tv = (TextView) LayoutInflater.from(mContext).inflate(  
  31.             android.R.layout.simple_list_item_1, parent, false);  
  32.         } else {  
  33.         tv = (TextView) convertView;  
  34.         }  
  35.         int colorPos = position % colors.length;  
  36.         tv.setBackgroundColor(colors[colorPos]);  
  37.         tv.setText(String.valueOf(position + 1) + ":"  
  38.             + inpormation[position]);  
  39.         return tv;  
  40.     }  
  41.    
  42.     private Context mContext;  
  43.     }  
  44.    
  45.     String[] inpormation = null;  
  46.     ListView listView;  
  47.    
  48.    @Override  
  49.    protected void onCreate(Bundle savedInstanceState) {  
  50.    readFile(R.raw.date0);  
  51.    setListAdapter(new MyListAdapter(this));  
  52.    listView = getListView();  
  53.    int[] colors = { 0, 0xFF505259, 0 };  
  54.    listView  
  55.        .setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));  
  56.    listView.setDividerHeight(10);  
  57.    super.onCreate(savedInstanceState);  
  58.    }  
  59.    
  60.    /** 
  61.     * 從raw中讀取數據 
  62.     * @param ID 
  63.     */  
  64.    public void readFile(int ID) {  
  65.    InputStream in = null;  
  66.    String temp = "";  
  67.    try {  
  68.        in = this.getResources().openRawResource(ID);  
  69.        byte[] buff = new byte[1024];// 緩存  
  70.        int rd = 0;  
  71.        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  72.        while ((rd = in.read(buff)) != -1) {  
  73.        baos.write(buff, 0, rd);  
  74.        temp = new String(baos.toByteArray(), "UTF-8");  
  75.        }  
  76.        baos.close();  
  77.        in.close();  
  78.        inpormation = temp.split("\r\n");  
  79.    } catch (Exception e) {  
  80.    Toast.makeText(this, "文件沒有找到", 2000).show();  
  81.    }  
  82.    }  
  83.    
  84. }  

四、在 SD 卡中處理數據

我們也可以把數據保存在 SD 卡中,做法是在 SD 卡上建立一個檔案來存放資料。需要說明的是,SD 卡是使用者可以直接存取的,因此適合存放一些可有可無的數據——即使使用者刪除了卡中的內容,也不會影響軟體的正常使用。

Java 程式碼:

  1. public class loadSDActivity extends Activity {  
  2.     public final static String FILE_NAME = "b.txt";  
  3.    @Override  
  4.    protected void onCreate(Bundle savedInstanceState) {  
  5.    setContentView(R.layout.sdfile);  
  6.    /**讀取內容**/  
  7.    String content = loadFile();  
  8.    if(content == null) {  
  9.        content = "上次沒有輸入內容請輸入";  
  10.    }  
  11.    
  12.    final EditText editContent = (EditText)findViewById(R.id.sdfile_et0);  
  13.    editContent.setHint("上次輸入SD卡的內容的為【 " +content + "】");  
  14.    Button button0 = (Button)findViewById(R.id.sdfile_button0);  
  15.    
  16.    /**監聽按鈕點擊後保存用戶輸入信息到SD卡中**/  
  17.    button0.setOnClickListener(new  OnClickListener() {  
  18.    
  19.        @Override  
  20.        public void onClick(View arg0) {  
  21.        /**拿到用戶輸入的信息**/  
  22.        String content = editContent.getText().toString();  
  23.        /**開始保存入SD卡**/  
  24.        saveFile(content);  
  25.        ShowDialog("保存SD卡文件成功");  
  26.        }  
  27.    });  
  28.    Button button1 = (Button)findViewById(R.id.sdfile_button1);  
  29.    
  30.    /**去清除SD卡保存的內容**/  
  31.    button1.setOnClickListener(new  OnClickListener() {  
  32.        @Override  
  33.        public void onClick(View arg0) {  
  34.        cleanFile();  
  35.        ShowDialog("清除SD卡文件中的內容成功");  
  36.        }  
  37.    });  
  38.    Button button2 = (Button)findViewById(R.id.sdfile_button2);  
  39.    
  40.    /**刪除SD卡保存的文件**/  
  41.    button2.setOnClickListener(new  OnClickListener() {  
  42.        @Override  
  43.        public void onClick(View arg0) {  
  44.        DeleteSDFile();  
  45.        }  
  46.    });  
  47.    
  48.    super.onCreate(savedInstanceState);  
  49.    }  
  50.    
  51.    /** 
  52.     * 保存入SD卡中 
  53.     * @param str 
  54.     */  
  55.    public void saveFile(String str) {  
  56.    FileOutputStream fileOutputStream = null;  
  57.    
  58.    File file = new File(Environment.getExternalStorageDirectory(),  
  59.        FILE_NAME);  
  60.    try {  
  61.    fileOutputStream = new FileOutputStream(file);  
  62.    fileOutputStream.write(str.getBytes());  
  63.    fileOutputStream.close();  
  64.    } catch (FileNotFoundException e) {  
  65.    e.printStackTrace();  
  66.    }catch (IOException e) {  
  67.    e.printStackTrace();  
  68.    }  
  69.    }  
  70.    
  71.    /** 
  72.     * 讀取SD卡的內容 
  73.     * @return 
  74.     */  
  75.    public String loadFile() {  
  76.    String path = Environment.getExternalStorageDirectory() + "/" + FILE_NAME;  
  77.    try {  
  78.    
  79.        FileInputStream fi = new FileInputStream(path);  
  80.        BufferedReader br = new BufferedReader(new InputStreamReader(  
  81.            fi));  
  82.        String readString = new String();  
  83.        while ((readString = br.readLine()) != null) {  
  84.        //數據多的話須要在這裡處理 readString  
  85.        return readString;  
  86.        }  
  87.        fi.close();  
  88.    } catch (FileNotFoundException e) {  
  89.    e.printStackTrace();  
  90.    } catch (IOException e) {  
  91.    e.printStackTrace();  
  92.    }  
  93.    
  94.    return null;  
  95.    }  
  96.    
  97.    /** 
  98.     * 刪除SD卡 
  99.     */  
  100.    public void DeleteSDFile() {  
  101.    String path = Environment.getExternalStorageDirectory() + "/"  
  102.        + FILE_NAME;  
  103.    File file1 = new File(path);  
  104.    boolean isdelte = file1.delete();  
  105.    if(isdelte) {  
  106.        ShowDialog("刪除SD卡成功");  
  107.    }else {  
  108.        finish();  
  109.    }  
  110.    }  
  111.    
  112.    /** 
  113.     * 因為java刪除文件內容只有一種實現方法,就是把整個文件重寫,只是把須要刪除的那一條記錄去除掉 
  114.     */  
  115.    public void cleanFile() {  
  116.    //如果只須要刪除文件中的一部分內容則須要在這裡對字符串做一些操作  
  117.    String cleanStr = "";  
  118.    FileOutputStream fileOutputStream = null;  
  119.    
  120.    File file = new File(Environment.getExternalStorageDirectory(),  
  121.        FILE_NAME);  
  122.    try {  
  123.    fileOutputStream = new FileOutputStream(file);  
  124.    fileOutputStream.write(cleanStr.getBytes());  
  125.    fileOutputStream.close();  
  126.    } catch (FileNotFoundException e) {  
  127.    e.printStackTrace();  
  128.    }catch (IOException e) {  
  129.    e.printStackTrace();  
  130.    }  
  131.    }  
  132.    public void ShowDialog(String str) {  
  133.    AlertDialog.Builder builder = new AlertDialog.Builder(loadSDActivity.this);  
  134.    builder.setIcon(R.drawable.icon);  
  135.    builder.setTitle(str);  
  136.    builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
  137.        public void onClick(DialogInterface dialog, int whichButton) {  
  138.        finish();  
  139.        }  
  140.    });  
  141.    builder.show();  
  142.    }  
  143. }  

XML 佈局:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.    
  3. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  4.    android:layout_width="fill_parent"  
  5.    android:layout_height="fill_parent"  
  6.    android:orientation="vertical"  
  7.    >  
  8.     <ImageView android:id="@+id/sdfile_image"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:src="@drawable/g"  
  12.         android:layout_gravity="center"  
  13.         />  
  14.     <EditText android:id="@+id/sdfile_et0"  
  15.               android:layout_width="fill_parent"  
  16.               android:layout_height="wrap_content"  
  17.               android:hint="請輸入需要保存到SD卡的內容">  
  18.     </EditText>  
  19.     <Button   android:id="@+id/sdfile_button0"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="保存輸入內容到SD卡">  
  23.     </Button>  
  24.     <Button   android:id="@+id/sdfile_button1"  
  25.         android:layout_width="wrap_content"  
  26.         android:layout_height="wrap_content"  
  27.         android:text="清除SD卡保存文件的內容">  
  28.     </Button>  
  29.     <Button   android:id="@+id/sdfile_button2"  
  30.         android:layout_width="wrap_content"  
  31.         android:layout_height="wrap_content"  
  32.         android:text="刪除SD卡中保存的文件">  
  33.     </Button>  
  34. </LinearLayout>  

以上就是四種常見本地數據儲存方案的完整示範。掌握它們各自的適用場景,你就能在開發中為每一類數據選擇最合適的存放方式。原始碼下載地址:https://vdisk.weibo.com/s/a9n91

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved