數據存儲與訪問
很多時候我們開發的軟件需要對處理后的數據進行訪問,以供再次訪問。
Android為數據存儲提供了如下幾種方式:
文件
SharedPreferences(參數)
SQLite函數庫
內容提供者(Content provider)
網絡

界面:

main.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/filename"
11 />
12 <EditText
13 android:layout_width="fill_parent"
14 android:layout_height="wrap_content"
15 android:inputType="text"
16 android:id="@+id/filename"
17 />
18 <TextView
19 android:layout_width="fill_parent"
20 android:layout_height="wrap_content"
21 android:text="@string/filecontent"
22 />
23 <EditText
24 android:layout_width="fill_parent"
25 android:layout_height="wrap_content"
26 android:minLines="3"
27 android:inputType="text"
28 android:id="@+id/filecontent"
29 />
30 <Button
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:text="@string/button"
34 android:id="@+id/button"
35 />
36
37 </LinearLayout>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/filename"
11 />
12 <EditText
13 android:layout_width="fill_parent"
14 android:layout_height="wrap_content"
15 android:inputType="text"
16 android:id="@+id/filename"
17 />
18 <TextView
19 android:layout_width="fill_parent"
20 android:layout_height="wrap_content"
21 android:text="@string/filecontent"
22 />
23 <EditText
24 android:layout_width="fill_parent"
25 android:layout_height="wrap_content"
26 android:minLines="3"
27 android:inputType="text"
28 android:id="@+id/filecontent"
29 />
30 <Button
31 android:layout_width="wrap_content"
32 android:layout_height="wrap_content"
33 android:text="@string/button"
34 android:id="@+id/button"
35 />
36
37 </LinearLayout>
string.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3
4 <string name="filename">文件名稱</string>
5 <string name="app_name">文件操作</string>
6 <string name="filecontent">文件內容</string>
7 <string name="button">保存</string>
8 <string name="success">保存成功!</string>
9 <string name="fail">保存失敗!</string>
10 </resources>
2 <resources>
3
4 <string name="filename">文件名稱</string>
5 <string name="app_name">文件操作</string>
6 <string name="filecontent">文件內容</string>
7 <string name="button">保存</string>
8 <string name="success">保存成功!</string>
9 <string name="fail">保存失敗!</string>
10 </resources>
FileActivity.java
1 package com.gaolei.file;
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.view.View;
6 import android.view.View.OnClickListener;
7 import android.widget.Button;
8 import android.widget.EditText;
9 import android.widget.Toast;
10
11 import com.gaolei.service.FileService;
12
13 public class FileActivity extends Activity {
14 private Button button;
15
16 /** Called when the activity is first created. */
17 @Override
18 public void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.main);
21 button = (Button) this.findViewById(R.id.button);
22 button.setOnClickListener(new ButtonclckListener());
23 }
24
25 public final class ButtonclckListener implements OnClickListener {
26
27 @Override
28 public void onClick(View v) {
29 EditText filenameText = (EditText) findViewById(R.id.filename);
30 EditText filecontentText = (EditText) findViewById(R.id.filecontent);
31 String filename = filenameText.getText().toString();
32 String content = filecontentText.getText().toString();
33
34 FileService fileService = new FileService(getApplicationContext());
35 try {
36 fileService.save(filename, content);
37 Toast.makeText(getApplicationContext(), R.string.success, 1)
38 .show();
39 } catch (Exception e) {
40 Toast.makeText(getApplicationContext(), R.string.fail, 1)
41 .show();
42 e.printStackTrace();
43 }
44
45 }
46
47 }
48 }
2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.view.View;
6 import android.view.View.OnClickListener;
7 import android.widget.Button;
8 import android.widget.EditText;
9 import android.widget.Toast;
10
11 import com.gaolei.service.FileService;
12
13 public class FileActivity extends Activity {
14 private Button button;
15
16 /** Called when the activity is first created. */
17 @Override
18 public void onCreate(Bundle savedInstanceState) {
19 super.onCreate(savedInstanceState);
20 setContentView(R.layout.main);
21 button = (Button) this.findViewById(R.id.button);
22 button.setOnClickListener(new ButtonclckListener());
23 }
24
25 public final class ButtonclckListener implements OnClickListener {
26
27 @Override
28 public void onClick(View v) {
29 EditText filenameText = (EditText) findViewById(R.id.filename);
30 EditText filecontentText = (EditText) findViewById(R.id.filecontent);
31 String filename = filenameText.getText().toString();
32 String content = filecontentText.getText().toString();
33
34 FileService fileService = new FileService(getApplicationContext());
35 try {
36 fileService.save(filename, content);
37 Toast.makeText(getApplicationContext(), R.string.success, 1)
38 .show();
39 } catch (Exception e) {
40 Toast.makeText(getApplicationContext(), R.string.fail, 1)
41 .show();
42 e.printStackTrace();
43 }
44
45 }
46
47 }
48 }
使用文件進行數據存儲
首先給大家介紹使用文件如何對數據進行存儲,Activity提供了openFileOutput()方法可以用于把數據輸出到文件中,具體的實現過程與在J2SE環境中保存數據到文件中是一樣的。
public class FileActivity extends Activity {
@Override public void onCreate(Bundle savedInstanceState) {
...
FileOutputStream outStream = this.openFileOutput("itcast.txt", Context.MODE_PRIVATE);
outStream.write("gaolei".getBytes());
outStream.close();
}
}
openFileOutput()方法的第一參數用于指定文件名稱,不能包含路徑分隔符“/” ,如果文件不存在,Android 會自動創建它。創建的文件保存在/data/data/<package name>/files目錄,如: /data/data/cn.itcast.action/files/itcast.txt ,通過點擊Eclipse菜單“Window”-“Show View”-“Other”,在對話窗口中展開android文件夾,選擇下面的File Explorer視圖,然后在File Explorer視圖中展開/data/data/<package name>/files目錄就可以看到該文件。
openFileOutput()方法的第二參數用于指定操作模式,有四種模式,分別為: Context.MODE_PRIVATE = 0
Context.MODE_APPEND = 32768
Context.MODE_PRIVATE:為默認操作模式,代表該文件是私有數據,只能被應用本身訪問,在該模式下,寫入的內容會覆蓋原文件的內容,如果想把新寫入的內容追加到原文件中。可以使用Context.MODE_APPEND
Context.MODE_APPEND:模式會檢查文件是否存在,存在就往文件追加內容,否則就創建新文件。
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應用是否有權限讀寫該文件。
MODE_WORLD_READABLE:表示當前文件可以被其他應用讀取;MODE_WORLD_WRITEABLE:表示當前文件可以被其他應用寫入。
如果希望文件被其他應用讀和寫,可以傳入:
openFileOutput("itcast.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
android有一套自己的安全模型,當應用程序(.apk)在安裝時系統就會分配給他一個userid,當該應用要去訪問其他資源比如文件的時候,就需要userid匹配。默認情況下,任何應用創建的文件,sharedpreferences,數據庫都應該是私有的(位于/data/data/<package name>/files),其他程序無法訪問。除非在創建時指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE ,只有這樣其他程序才能正確訪問。
業務類:
1 package com.gaolei.service;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7
8 import android.content.Context;
9
10 public class FileService {
11
12 private Context context;
13
14 public FileService(Context context) {
15 this.context = context;
16 }
17
18 /**
19 * 保存文件
20 *
21 * @param filename
22 * 文件名稱
23 * @param content
24 * 文件內容
25 * @throws IOException
26 */
27 public void save(String filename, String content) throws Exception {
28 // 私有操作模式:創建出來的文件只能被本應用訪問,其他應用無法訪問該文件,
29 // 另外采用私有操作模式創建的文件,寫入文件中的內容會覆蓋原文件的內容
30 FileOutputStream outStream = context.openFileOutput(filename,
31 Context.MODE_PRIVATE);
32 outStream.write(filename.getBytes());
33 outStream.close();
34 }
35
36 /**
37 * 讀取文件內容
38 *
39 * @param filename
40 * 文件名稱
41 * @return 文件內容
42 * @throws Exception
43 */
44 public String read(String filename) throws Exception {
45 FileInputStream inStream = context.openFileInput(filename);
46 byte[] buffer = new byte[1024];
47 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
48 int len = 0;
49 while ((len = inStream.read(buffer)) != -1) {
50 outStream.write(buffer, 0, len);
51 }
52 byte[] data = outStream.toByteArray();
53 outStream.close();
54 inStream.close();
55 return new String(data);
56 }
57
58 }
59
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7
8 import android.content.Context;
9
10 public class FileService {
11
12 private Context context;
13
14 public FileService(Context context) {
15 this.context = context;
16 }
17
18 /**
19 * 保存文件
20 *
21 * @param filename
22 * 文件名稱
23 * @param content
24 * 文件內容
25 * @throws IOException
26 */
27 public void save(String filename, String content) throws Exception {
28 // 私有操作模式:創建出來的文件只能被本應用訪問,其他應用無法訪問該文件,
29 // 另外采用私有操作模式創建的文件,寫入文件中的內容會覆蓋原文件的內容
30 FileOutputStream outStream = context.openFileOutput(filename,
31 Context.MODE_PRIVATE);
32 outStream.write(filename.getBytes());
33 outStream.close();
34 }
35
36 /**
37 * 讀取文件內容
38 *
39 * @param filename
40 * 文件名稱
41 * @return 文件內容
42 * @throws Exception
43 */
44 public String read(String filename) throws Exception {
45 FileInputStream inStream = context.openFileInput(filename);
46 byte[] buffer = new byte[1024];
47 ByteArrayOutputStream outStream = new ByteArrayOutputStream();
48 int len = 0;
49 while ((len = inStream.read(buffer)) != -1) {
50 outStream.write(buffer, 0, len);
51 }
52 byte[] data = outStream.toByteArray();
53 outStream.close();
54 inStream.close();
55 return new String(data);
56 }
57
58 }
59

讀取文件內容
如果要打開存放在/data/data/<package name>/files目錄應用私有的文件,可以使用Activity提供openFileInput()方法。
FileInputStream inStream = this.getContext().openFileInput("itcast.txt");
Log.i("FileTest", readInStream(inStream));
或者直接使用文件的絕對路徑:
File file = new File("/data/data/cn.itcast.action/files/itcast.txt");
FileInputStream inStream = new FileInputStream(file);
Log.i("FileTest", readInStream(inStream));
注意:上面文件路徑中的“cn.itcast.action”為應用所在包,當你在編寫代碼時應替換為你自己應用使用的包。
對于私有文件只能被創建該文件的應用訪問,如果希望文件能被其他應用讀和寫,可以在創建文件時,指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE權限。
Activity還提供了getCacheDir()和getFilesDir()方法:
getCacheDir()方法用于獲取/data/data/<package name>/cache目錄
getFilesDir()方法用于獲取/data/data/<package name>/files目錄
Context.MODE_WORLD_READABLE = 1
Context.MODE_WORLD_WRITEABLE = 2
AndroidMainFest.xml
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.gaolei.file"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk android:minSdkVersion="15" />
8
9 <application
10 android:icon="@drawable/ic_launcher"
11 android:label="@string/app_name" >
12 <activity
13 android:name=".FileActivity"
14 android:label="@string/app_name" >
15 <intent-filter>
16 <action android:name="android.intent.action.MAIN" />
17
18 <category android:name="android.intent.category.LAUNCHER" />
19 </intent-filter>
20 </activity>
21 <uses-library android:name="android.test.runner"/>
22 </application>
23 <instrumentation android:name="android.test.InstrumentationTestRunner"
24 android:targetPackage="com.gaolei.file" android:label="Tests for My Apps">
25 </instrumentation>
26
27 </manifest>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.gaolei.file"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk android:minSdkVersion="15" />
8
9 <application
10 android:icon="@drawable/ic_launcher"
11 android:label="@string/app_name" >
12 <activity
13 android:name=".FileActivity"
14 android:label="@string/app_name" >
15 <intent-filter>
16 <action android:name="android.intent.action.MAIN" />
17
18 <category android:name="android.intent.category.LAUNCHER" />
19 </intent-filter>
20 </activity>
21 <uses-library android:name="android.test.runner"/>
22 </application>
23 <instrumentation android:name="android.test.InstrumentationTestRunner"
24 android:targetPackage="com.gaolei.file" android:label="Tests for My Apps">
25 </instrumentation>
26
27 </manifest>
測試類:
1 package com.gaolei.test;
2
3 import com.gaolei.service.FileService;
4
5 import android.test.AndroidTestCase;
6 import android.util.Log;
7
8 public class FileServiceTest extends AndroidTestCase {
9
10 private static final String TAG = "FileServiceTest";
11
12 public void testRead() throws Throwable {
13 FileService fileService = new FileService(this.getContext());
14 String result = fileService.read("gaolei.txt");
15 Log.i(TAG, result);
16 }
17 }
2
3 import com.gaolei.service.FileService;
4
5 import android.test.AndroidTestCase;
6 import android.util.Log;
7
8 public class FileServiceTest extends AndroidTestCase {
9
10 private static final String TAG = "FileServiceTest";
11
12 public void testRead() throws Throwable {
13 FileService fileService = new FileService(this.getContext());
14 String result = fileService.read("gaolei.txt");
15 Log.i(TAG, result);
16 }
17 }
