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方法實現的。
點擊(此處)摺疊或打開
- package com.example.warrior.sharedpreferencetest;
- import android.app.Activity;
- import android.content.SharedPreferences;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ImageButton;
- public class MainActivity
extends Activity
{
- private EditText user
= null;
- private EditText password
= null;
- private ImageButton loginBtn
= null;
- @Override
- protected
void onCreate(Bundle savedInstanceState)
{
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- user =
(EditText)findViewById(R.id.user);
- password =
(EditText)findViewById(R.id.pass);
- loginBtn =
(ImageButton)findViewById(R.id.loginButton);
- initView();
- loginBtn.setOnTouchListener(new
View.OnTouchListener()
{
- @Override
- public
boolean onTouch(View v, MotionEvent
event)
{
- if(event.getAction()==MotionEvent.ACTION_DOWN){
- v.setBackgroundResource(R.drawable.b2);
- SharedPreferences userinfo = getSharedPreferences("user_info",0);
- userinfo.edit().putString("name",user.getText().toString()).commit();
- userinfo.edit().putString("pass",password.getText().toString()).commit();
- }
- else
if(event.getAction()==MotionEvent.ACTION_UP){
- v.setBackgroundResource(R.drawable.b3);
- }
- return false;
- }
- });
- }
- private void initView(){
- SharedPreferences userInfo = getSharedPreferences("user_info",0);
- String username
= userInfo.getString("name","");
- String pass
= userInfo.getString("pass","");
- user.setText(username);
- password.setText(pass);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu
menu)
{
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main,
menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem
item)
{
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id
= item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id
== R.id.action_settings)
{
- return true;
- }
- return
super.onOptionsItemSelected(item);
- }
- }
點擊(此處)摺疊或打開
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:orientation="vertical"
- android:background="@drawable/b1"
- tools:context=".MainActivity">
- <EditText
- android:layout_width="185dp"
- android:layout_height="40dp"
- android:id="@+id/user"
- android:hint="enter your name"
- android:singleLine="true"
- android:layout_alignParentTop="true"
- android:layout_alignLeft="@+id/pass"
- android:layout_marginTop="66dp"
- />
- <EditText
- android:layout_width="185dp"
- android:layout_height="40dp"
- android:id="@+id/pass"
- android:hint="enter your password"
- android:singleLine="true"
- android:layout_below="@+id/user"
- android:layout_centerHorizontal="true"
- android:layout_marginTop="44dp"
- android:inputType="textPassword"
- />
- <ImageButton
- android:layout_width="100dp"
- android:layout_height="60dp"
- android:id="@+id/loginButton"
- android:background="@drawable/ibtn"
- android:layout_centerVertical="true"
- android:layout_alignRight="@+id/pass"
- android:layout_marginRight="20dp"
- />
- </RelativeLayout>
寫操作-》通過FileOutputStream fWriteStream = openFileOutput(filename,MODE_APPEND);聲稱文件寫入對象,通過fWriteStream.write(buffer);方法寫入,然後關閉fWriteStream.close();
讀操作-》通過FileInputStream fInputStream = openFileInput(filename);創建讀操作對象,通過fInputStream.read(buffer);方法讀取內容,然後fInputStream.close();關閉。
代碼如下
.xml
點擊(此處)摺疊或打開
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context=".MainActivity">
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:id="@+id/text"
- />
- </RelativeLayout>
點擊(此處)摺疊或打開
- package com.example.warrior.filestreamtest;
- import android.app.Activity;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- public class MainActivity
extends Activity
{
- @Override
- protected
void onCreate(Bundle savedInstanceState)
{
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- String fileName
= "fileStreamText.txt";
- String fileContent
= "welcome to android!!"
+"\n\r"+
- "I love it!!";
- String
result = "";
- boolean istrue
= writeFile(fileName,fileContent);
- if(istrue){
- result
+= fileName+"create success!!\n\r";
- }else
{
- result+= fileName+"create failure!!\n\r";
- }
- result
+= readFile(fileName);
- TextView textView =
(TextView)findViewById(R.id.text);
- textView.setText(result);
- }
- /*write content to new file
- *param filename: name of new file
- * param content:the content to be writen into the file
- * return boolean:true/success write into false/failure write into
- */
- public boolean writeFile(String filename
,String content){
- try {
- FileOutputStream fWriteStream
= openFileOutput(filename,MODE_APPEND);
- byte[]
buffer = content.getBytes();
- fWriteStream.write(buffer);
- fWriteStream.flush();
- fWriteStream.close();
- return true;
- }catch
(Exception e){
- e.printStackTrace();
- return false;
- }
- }
- /*read content from file
- *param filename:filename
- * return String
- */
- public String readFile(String filename){
- String readBuffer
= "";
- try {
- FileInputStream fInputStream
= openFileInput(filename);
- int len
= fInputStream.available();
- byte[]
buffer =
new byte[len];
- fInputStream.read(buffer);
- fInputStream.close();
- readBuffer =
new String(buffer);
- }catch
(Exception e){
- e.printStackTrace();
- }
- return readBuffer;
- }
- @Override
- public boolean onCreateOptionsMenu(Menu
menu)
{
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main,
menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem
item)
{
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id
= item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id
== R.id.action_settings)
{
- return true;
- }
- return
super.onOptionsItemSelected(item);
- }
- }
閱讀(7) | 評論(0) | 轉發(0) |
- mysql不能導入和創建存儲過程...
- 在 CentOS 7.1 上安裝分佈式存...
- 開發基於高德SDK的Android車載...
- 製作多版本 Fedora 系統安裝 U...
- WPF Image賦值的兩種方法...