android 存儲結構與存儲方法


1. android的存儲方式主要分爲:
1)本地的文本存儲;2)數據庫存儲
    存儲方法:1)SharedPreference 存儲:適用於簡單的數據保存例如屬性文件
                    2)文件存儲數據:常用方式,可保存較大數據,可存儲在系統或者SD卡中
                    3)SQLite數據庫存儲:以數據庫形式存儲數據
                    4)ContentProvider存儲數據:爲存儲和獲取提供統一接口,主要用於程序間數據共享
                    5)網絡存儲:通過網絡來存儲獲取數據,主要應用在網絡相關的應用中
2. 文件結構
    1)系統文件:主要存儲在\system文件夾下,子文件夾有,app,bin,etc,media等。更改讀取等操作需要roots權限
    2)數據文件:主要存儲在\data文件夾下,子文件夾有,app,backup,data等,主要存儲着應用程序以及應用中產生的臨時數據等信息。data文件夾下的沒有權限的程序不能相互訪問數據,保護私有數據
    3)外部存儲:對於較大的文件一般會存儲在SD卡等外部存儲中。只要有訪問SD卡權限就能夠訪問其中所有文件,數據安全性較低
3. 5種測試方法的實例
     1)SharedPreference 存儲:以一個登陸界面來演示
     如下是.xml 和.java 文件代碼
     程序中將用戶名user 和密碼 pass 通過SharedPreference 存儲,當再次打開應用時,由initView();方法把上次的配置信息直接顯示在界面上
     通過SharedPreferences userinfo = getSharedPreferences("user_info",0);獲得SharedPreferences對象
     通過
userinfo.edit().putString("name",user.getText().toString()).commit();存儲數據,利用對象的edit接口的putString方法保存,最終的提交是通過commit()來完成的
     在initView();中可以看到,獲取數據是通過SharedPreferences對象userinfo的getString方法實現的。

點擊(此處)摺疊或打開

  1. package com.example.warrior.sharedpreferencetest;

  2. import android.app.Activity;
  3. import android.content.SharedPreferences;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.MotionEvent;
  9. import android.view.View;
  10. import android.widget.EditText;
  11. import android.widget.ImageButton;


  12. public class MainActivity extends Activity {
  13.     private EditText user = null;
  14.     private EditText password = null;
  15.     private ImageButton loginBtn = null;
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);
  20.         user = (EditText)findViewById(R.id.user);
  21.         password = (EditText)findViewById(R.id.pass);
  22.         loginBtn = (ImageButton)findViewById(R.id.loginButton);
  23.         initView();
  24.         loginBtn.setOnTouchListener(new View.OnTouchListener() {
  25.             @Override
  26.             public boolean onTouch(View v, MotionEvent event) {
  27.                if(event.getAction()==MotionEvent.ACTION_DOWN){
  28.                    v.setBackgroundResource(R.drawable.b2);
  29.                    SharedPreferences userinfo = getSharedPreferences("user_info",0);
  30.                    userinfo.edit().putString("name",user.getText().toString()).commit();
  31.                    userinfo.edit().putString("pass",password.getText().toString()).commit();
  32.                }
  33.                 else if(event.getAction()==MotionEvent.ACTION_UP){
  34.                    v.setBackgroundResource(R.drawable.b3);
  35.                }
  36.                 return false;

  37.             }
  38.         });
  39.     }
  40.     private void initView(){
  41.         SharedPreferences userInfo = getSharedPreferences("user_info",0);
  42.         String username = userInfo.getString("name","");
  43.         String pass = userInfo.getString("pass","");
  44.         user.setText(username);
  45.         password.setText(pass);
  46.     }

  47.     @Override
  48.     public boolean onCreateOptionsMenu(Menu menu) {
  49.         // Inflate the menu; this adds items to the action bar if it is present.
  50.         getMenuInflater().inflate(R.menu.menu_main, menu);
  51.         return true;
  52.     }

  53.     @Override
  54.     public boolean onOptionsItemSelected(MenuItem item) {
  55.         // Handle action bar item clicks here. The action bar will
  56.         // automatically handle clicks on the Home/Up button, so long
  57.         // as you specify a parent activity in AndroidManifest.xml.
  58.         int id = item.getItemId();

  59.         //noinspection SimplifiableIfStatement
  60.         if (id == R.id.action_settings) {
  61.             return true;
  62.         }

  63.         return super.onOptionsItemSelected(item);
  64.     }
  65. }

點擊(此處)摺疊或打開

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"
  6.     android:paddingRight="@dimen/activity_horizontal_margin"
  7.     android:paddingTop="@dimen/activity_vertical_margin"
  8.     android:paddingBottom="@dimen/activity_vertical_margin"
  9.     android:orientation="vertical"
  10.     android:background="@drawable/b1"
  11.     tools:context=".MainActivity">

  12.     <EditText
  13.         android:layout_width="185dp"
  14.         android:layout_height="40dp"
  15.         android:id="@+id/user"
  16.         android:hint="enter your name"
  17.         android:singleLine="true"
  18.         android:layout_alignParentTop="true"
  19.         android:layout_alignLeft="@+id/pass"
  20.         android:layout_marginTop="66dp"
  21.         />
  22.     <EditText
  23.         android:layout_width="185dp"
  24.         android:layout_height="40dp"
  25.         android:id="@+id/pass"
  26.         android:hint="enter your password"
  27.         android:singleLine="true"
  28.         android:layout_below="@+id/user"
  29.         android:layout_centerHorizontal="true"
  30.         android:layout_marginTop="44dp"
  31.         android:inputType="textPassword"
  32.         />
  33.     <ImageButton
  34.         android:layout_width="100dp"
  35.         android:layout_height="60dp"
  36.         android:id="@+id/loginButton"
  37.         android:background="@drawable/ibtn"
  38.         android:layout_centerVertical="true"
  39.         android:layout_alignRight="@+id/pass"
  40.         android:layout_marginRight="20dp"
  41.         />

  42. </RelativeLayout>
    2)文件存儲數據:本示例以創建文件,寫入內容,讀取內容,顯示爲主線
     寫操作-》通過FileOutputStream fWriteStream = openFileOutput(filename,MODE_APPEND);聲稱文件寫入對象,通過fWriteStream.write(buffer);方法寫入,然後關閉fWriteStream.close();
    讀操作-》通過FileInputStream fInputStream = openFileInput(filename);創建讀操作對象,通過fInputStream.read(buffer);方法讀取內容,然後fInputStream.close();關閉。

     代碼如下
    .xml

點擊(此處)摺疊或打開

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"
  6.     android:paddingRight="@dimen/activity_horizontal_margin"
  7.     android:paddingTop="@dimen/activity_vertical_margin"
  8.     android:paddingBottom="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity">
  10. <TextView
  11.     android:layout_width="match_parent"
  12.     android:layout_height="wrap_content"
  13.     android:id="@+id/text"
  14.     />


  15. </RelativeLayout>
    .java
    

點擊(此處)摺疊或打開

  1. package com.example.warrior.filestreamtest;

  2. import android.app.Activity;
  3. import android.support.v7.app.ActionBarActivity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.widget.TextView;

  8. import java.io.FileInputStream;
  9. import java.io.FileOutputStream;


  10. public class MainActivity extends Activity {

  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         setContentView(R.layout.activity_main);
  15.         String fileName = "fileStreamText.txt";
  16.         String fileContent = "welcome to android!!" +"\n\r"+
  17.                 "I love it!!";
  18.         String result = "";
  19.         boolean istrue = writeFile(fileName,fileContent);
  20.         if(istrue){
  21.             result += fileName+"create success!!\n\r";
  22.         }else {
  23.             result+= fileName+"create failure!!\n\r";
  24.         }
  25.         result += readFile(fileName);
  26.         TextView textView = (TextView)findViewById(R.id.text);
  27.         textView.setText(result);
  28.     }

  29.     /*write content to new file
  30.     *param filename: name of new file
  31.     * param content:the content to be writen into the file
  32.     * return boolean:true/success write into false/failure write into
  33.      */
  34.     public boolean writeFile(String filename ,String content){
  35.         try {
  36.             FileOutputStream fWriteStream = openFileOutput(filename,MODE_APPEND);
  37.             byte[] buffer = content.getBytes();
  38.             fWriteStream.write(buffer);
  39.             fWriteStream.flush();
  40.             fWriteStream.close();
  41.             return true;
  42.         }catch (Exception e){
  43.             e.printStackTrace();
  44.             return false;
  45.         }
  46.     }
  47.     /*read content from file
  48.     *param filename:filename
  49.     * return String
  50.      */
  51.     public String readFile(String filename){
  52.         String readBuffer = "";
  53.         try {
  54.             FileInputStream fInputStream = openFileInput(filename);
  55.             int len = fInputStream.available();
  56.             byte[] buffer = new byte[len];
  57.             fInputStream.read(buffer);
  58.             fInputStream.close();
  59.             readBuffer = new String(buffer);
  60.         }catch (Exception e){
  61.             e.printStackTrace();
  62.         }
  63.         return readBuffer;
  64.     }
  65.     @Override
  66.     public boolean onCreateOptionsMenu(Menu menu) {
  67.         // Inflate the menu; this adds items to the action bar if it is present.
  68.         getMenuInflater().inflate(R.menu.menu_main, menu);
  69.         return true;
  70.     }

  71.     @Override
  72.     public boolean onOptionsItemSelected(MenuItem item) {
  73.         // Handle action bar item clicks here. The action bar will
  74.         // automatically handle clicks on the Home/Up button, so long
  75.         // as you specify a parent activity in AndroidManifest.xml.
  76.         int id = item.getItemId();

  77.         //noinspection SimplifiableIfStatement
  78.         if (id == R.id.action_settings) {
  79.             return true;
  80.         }

  81.         return super.onOptionsItemSelected(item);
  82.     }
  83. }



閱讀(7) | 評論(0) | 轉發(0) |
評論熱議
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章