声明:文章原创,转载需注明出处。由于文章大多是学习过程中的记录内容,技术能力有限,希望大家指出其中错误,共同交流进步。由此原因,文章会不定期改善,看原文最新内容,请到: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方法实现的。
-
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=" />
-
xmlns: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>
2)文件存储数据:本示例以创建文件,写入内容,读取内容,显示为主线
写操作-》通过
FileOutputStream fWriteStream
= openFileOutput
(filename
,MODE_APPEND
);声称文件写入对象,通过fWriteStream
.write(buffer);方法写入,然后关闭fWriteStream
.close();
读操作-》通过
FileInputStream fInputStream
= openFileInput
(filename
);创建读操作对象,通过fInputStream
.read(buffer);方法读取内容,然后
fInputStream.close();关闭。
代码如下
.xml
-
<RelativeLayout xmlns:android=" />
-
xmlns: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>
.java
-
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);
-
}
-
}
阅读(1291) | 评论(0) | 转发(0) |