Chinaunix首页 | 论坛 | 博客
  • 博客访问: 291947
  • 博文数量: 82
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 874
  • 用 户 组: 普通用户
  • 注册时间: 2015-03-21 09:58
个人简介

traveling in cumputer science!!

文章分类

全部博文(82)

文章存档

2016年(13)

2015年(69)

我的朋友

分类: Android平台

2015-07-18 13:14:31

声明:文章原创,转载需注明出处。由于文章大多是学习过程中的记录内容,技术能力有限,希望大家指出其中错误,共同交流进步。由此原因,文章会不定期改善,看原文最新内容,请到:http://blog.chinaunix.net/uid/29454152.html
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=" />
  2.     xmlns: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=" />
  2.     xmlns: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. }



阅读(1291) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~