數(shù)據(jù)存儲(chǔ)與訪問
很多時(shí)候我們開發(fā)的軟件需要對(duì)處理后的數(shù)據(jù)進(jìn)行訪問,以供再次訪問。
Android為數(shù)據(jù)存儲(chǔ)提供了如下幾種方式:
文件
SharedPreferences(參數(shù))
SQLite函數(shù)庫
內(nèi)容提供者(Content provider)
網(wǎng)絡(luò)
界面:
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>
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">文件內(nèi)容</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 }
使用文件進(jìn)行數(shù)據(jù)存儲(chǔ)
首先給大家介紹使用文件如何對(duì)數(shù)據(jù)進(jìn)行存儲(chǔ),Activity提供了openFileOutput()方法可以用于把數(shù)據(jù)輸出到文件中,具體的實(shí)現(xiàn)過程與在J2SE環(huán)境中保存數(shù)據(jù)到文件中是一樣的。
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()方法的第一參數(shù)用于指定文件名稱,不能包含路徑分隔符“/” ,如果文件不存在,Android 會(huì)自動(dòng)創(chuàng)建它。創(chuàng)建的文件保存在/data/data/<package name>/files目錄,如: /data/data/cn.itcast.action/files/itcast.txt ,通過點(diǎn)擊Eclipse菜單“Window”-“Show View”-“Other”,在對(duì)話窗口中展開android文件夾,選擇下面的File Explorer視圖,然后在File Explorer視圖中展開/data/data/<package name>/files目錄就可以看到該文件。
openFileOutput()方法的第二參數(shù)用于指定操作模式,有四種模式,分別為: Context.MODE_PRIVATE = 0
Context.MODE_APPEND = 32768
Context.MODE_PRIVATE:為默認(rèn)操作模式,代表該文件是私有數(shù)據(jù),只能被應(yīng)用本身訪問,在該模式下,寫入的內(nèi)容會(huì)覆蓋原文件的內(nèi)容,如果想把新寫入的內(nèi)容追加到原文件中。可以使用Context.MODE_APPEND
Context.MODE_APPEND:模式會(huì)檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件。
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應(yīng)用是否有權(quán)限讀寫該文件。
MODE_WORLD_READABLE:表示當(dāng)前文件可以被其他應(yīng)用讀取;MODE_WORLD_WRITEABLE:表示當(dāng)前文件可以被其他應(yīng)用寫入。
如果希望文件被其他應(yīng)用讀和寫,可以傳入:
openFileOutput("itcast.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
android有一套自己的安全模型,當(dāng)應(yīng)用程序(.apk)在安裝時(shí)系統(tǒng)就會(huì)分配給他一個(gè)userid,當(dāng)該應(yīng)用要去訪問其他資源比如文件的時(shí)候,就需要userid匹配。默認(rèn)情況下,任何應(yīng)用創(chuàng)建的文件,sharedpreferences,數(shù)據(jù)庫都應(yīng)該是私有的(位于/data/data/<package name>/files),其他程序無法訪問。除非在創(chuàng)建時(shí)指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE ,只有這樣其他程序才能正確訪問。
業(yè)務(wù)類:
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 * 文件內(nèi)容
25 * @throws IOException
26 */
27 public void save(String filename, String content) throws Exception {
28 // 私有操作模式:創(chuàng)建出來的文件只能被本應(yīng)用訪問,其他應(yīng)用無法訪問該文件,
29 // 另外采用私有操作模式創(chuàng)建的文件,寫入文件中的內(nèi)容會(huì)覆蓋原文件的內(nèi)容
30 FileOutputStream outStream = context.openFileOutput(filename,
31 Context.MODE_PRIVATE);
32 outStream.write(filename.getBytes());
33 outStream.close();
34 }
35
36 /**
37 * 讀取文件內(nèi)容
38 *
39 * @param filename
40 * 文件名稱
41 * @return 文件內(nèi)容
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
讀取文件內(nèi)容
如果要打開存放在/data/data/<package name>/files目錄應(yīng)用私有的文件,可以使用Activity提供openFileInput()方法。
FileInputStream inStream = this.getContext().openFileInput("itcast.txt");
Log.i("FileTest", readInStream(inStream));
或者直接使用文件的絕對(duì)路徑:
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”為應(yīng)用所在包,當(dāng)你在編寫代碼時(shí)應(yīng)替換為你自己應(yīng)用使用的包。
對(duì)于私有文件只能被創(chuàng)建該文件的應(yīng)用訪問,如果希望文件能被其他應(yīng)用讀和寫,可以在創(chuàng)建文件時(shí),指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE權(quán)限。
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>
測試類:
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 }