今年看了下android的文件存储,大致和java实现差不多
android提供了如下两个方法来打开本应用程序的数据文件的IO流:
FileInputStream openFileInput(fileName); //输入流
FileOutputStream outputStream = openFileOutput(fileName, MODE_PRIVATE); //输出流
打开输出流的方法的第二个是表示打开文件的mode,大概有一下几种:
MODE_PRIVATE : 该文件只能被当前程序读写,且是覆盖的写
MODE_APPEND:以追加的方式打开文件,可向该文件中添加内容
MODE_WORLD_READABLE:该文件的内容可以被其他应用程序读
MODE_WORLD_WRITEABLE:该文件的内容可以被其他程序读、写
下面简单实现从一个Edittext中读取输入的文字,在Textview中显示出来
MainActivity.java
package com.file.filetest; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import com.file.filetest.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class MainActivity extends Activity { public static String FILE_NAME = "Testfile.bin"; private EditText editText; private TextView textView; private Button readButton; private Button writeButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); readButton = (Button) findViewById(R.id.button2); writeButton = (Button) findViewById(R.id.button1); editText = (EditText) findViewById(R.id.editText1); textView = (TextView) findViewById(R.id.textView1); readButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { textView.setText(readFile(FILE_NAME)); } }); writeButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { writeFile(FILE_NAME, editText.getText().toString()); } }); } private String readFile(String fileName) { BufferedReader bReader = null; try { FileInputStream inStream = openFileInput(fileName); InputStreamReader reader = new InputStreamReader(inStream, "GBK"); bReader = new BufferedReader(reader); StringBuffer stringBuffer = new StringBuffer(""); String str; while ((str = bReader.readLine()) != null) { stringBuffer.append(str); } return stringBuffer.toString(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (bReader != null) { try { bReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return ""; } private void writeFile(String fileName, String context) { BufferedWriter writer = null; FileOutputStream outputStream; try { outputStream = openFileOutput(fileName, MODE_PRIVATE); OutputStreamWriter outputWriter = new OutputStreamWriter( outputStream, "GBK"); writer = new BufferedWriter(outputWriter); writer.write(context); writer.newLine(); writer.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
布局文件main.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" tools:context="${relativePackage}.${activityClass}" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/editText1" android:text="TextView" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="98dp" android:text="写入" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_centerHorizontal="true" android:text="读取" /> </RelativeLayout>
布局效果图如下:
以上基本实现了文件的读写功能,但是这只能满足输入的仅仅只是文字和数字的情况下,当输入图片(比如调用输入法输入一个笑脸)时,就有问题了,在网上大致搜了下,并没有找到比较好的解决方法,图文混排存入文件,有4个问题要解决:
- 从EditText中识别出输入法输入的Bitmap,并记录
- 将文字和Bitmap存入文件,bitmap要单独编码
- 从文件中解析出文字信息和bitmap(解码)
- 在textview中 图文混合显示从文件中读出的文字和图片 -- 解决方法 利用SpannableString类
期待大神的解答
时间: 2024-12-22 00:59:05