论坛首页 移动开发技术论坛

Android 文件存储--内部存储的例子

浏览 3697 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-03-16  
                                 Android 文件存储--内部存储的例子

1)新建Android 项目,项目名称:DemoInternalStorage
2) 在继承于Activity的类中编写相应代码,代码如下所示:
/*
* Copyright (C) Mesada Technologies Co., Ltd. 2005-2010.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Mesada Technologies Co., Ltd. ("Confidential Information").
* You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement
* you entered into with Mesada.
*/
package com.mesada.demo;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
* This is a demo about file storage.
*
* @author Xiaolong Long
* @date 2010-12-30
* @version 1.0
*/
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "MainActivity";
private static final boolean mIsPrintInfo = true;

private static final String FILENAME = "temp.txt";

EditText mMsgView;
Button mSave;
Button mPrint;
Button mCancel;

boolean mIsLegal = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
if (mIsPrintInfo)
Log.i(TAG, "onCreate()...");

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupControlers();

mSave.setEnabled(false);
MsgTextWater msgTextWater = new MsgTextWater();
mMsgView.addTextChangedListener(msgTextWater);

mSave.setOnClickListener(this);
mPrint.setOnClickListener(this);
mCancel.setOnClickListener(this);
}

/**
*
* Find the views that were identified by the id attributes from the XML.
*
* @param
* @return
* @date 2010-12-30
* @author Xiaolong Long
*/
private void setupControlers() {
if (mIsPrintInfo)
Log.i(TAG, "setupControlers()...");

mMsgView = (EditText) findViewById(R.id.msg);
mSave = (Button) findViewById(R.id.saveMsg);
mPrint = (Button) findViewById(R.id.printMsg);
mCancel = (Button) findViewById(R.id.cancel);
}


/**
*
* Find the views that were identified by the id attributes from the XML.
*
* @param
* @return
* @date 2010-12-30
* @author Xiaolong Long
*/
private void setupControlers() {
if (mIsPrintInfo)
Log.i(TAG, "setupControlers()...");

mMsgView = (EditText) findViewById(R.id.msg);
mSave = (Button) findViewById(R.id.saveMsg);
mPrint = (Button) findViewById(R.id.printMsg);
mCancel = (Button) findViewById(R.id.cancel);
}

public void onClick(View v) {
if (mIsPrintInfo)
Log.i(TAG, "onClick()...");

// Returns this view's identifier.
int id = v.getId();
switch (id) {
case R.id.saveMsg:
try {
saveMsg();
Toast.makeText(MainActivity.this, R.string.success_write,
Toast.LENGTH_SHORT).show();
mMsgView.setText("");
} catch (IOException e) {
Log.e(TAG,
"failed to save the content to the file which called temp.txt",
e);
Toast.makeText(MainActivity.this, R.string.failed_write,
Toast.LENGTH_SHORT).show();
}
break;
case R.id.printMsg:
try {
mMsgView.requestFocus();
mMsgView.setText(getMsg());
} catch (IOException e) {
Log.e(TAG,
"failed to read a file from internal storage which called temp.txt",
e);
Toast.makeText(MainActivity.this, R.string.failed_read,
Toast.LENGTH_SHORT).show();
}
break;
case R.id.cancel:
finish();
break;
default:
break;
}
}
/**
*
* To create and write a file to the internal storage.
*
* @param
* @return
* @date 2010-12-30
* @author Xiaolong Long
*/
private void saveMsg() throws IOException {
String msg = String.valueOf(mMsgView.getText());
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_APPEND);
// To create and write a private file to the internal storage:
fos.write(msg.getBytes("utf-8"));
fos.flush();
fos.close();
}

/**
* To read a file from internal storage.
*
* @param
* @return
* @date 2010-12-30
* @author Xiaolong Long
*/
private String getMsg() throws IOException {
FileInputStream fis = openFileInput(FILENAME);
int length = FILENAME.length();
byte[] buffer = new byte[length];

ByteArrayOutputStream bos = new ByteArrayOutputStream();
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
fis.close();
bos.close();
return bos.toString();
}
class MsgTextWater implements TextWatcher {

public void afterTextChanged(Editable s) {

}

public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}

public void onTextChanged(CharSequence s, int start, int before,
int count) {
mIsLegal = validate(s);
if (mIsLegal) {
mSave.setEnabled(true);
return;
}
mSave.setEnabled(false);
}

/**
*
* To check the view if legal.
*
* @param
* @return
* @date 2010-12-30
* @author Xiaolong Long
*/
private boolean validate(CharSequence s) {
String ss = s.toString();
if (!"".equals(ss)) {
return true;
}
return false;
}
}
}
3)main.xml 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/enter_msg" />
<EditText
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right">
<Button
android:id="@+id/saveMsg"
android:text="@string/savemsg"
android:layout_width="180px"
android:layout_height="wrap_content"></Button>
<Button
android:id="@+id/printMsg"
android:text="@string/printmsg"
android:layout_width="145px"
android:layout_height="wrap_content"></Button>
<Button
android:id="@+id/cancel"
android:text="@string/cancel"
android:layout_width="145px"
android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>

4)AndroidMainfest.xml 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.mesada.demo"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />

</manifest>
5)完成,顺便上传截图:

  • 大小: 29.8 KB
   发表时间:2011-03-23  
代码还是格式一下撒
0 请登录后投票
   发表时间:2011-03-27  
...你这存的是什么? text?就这个?
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics