Выбрать главу

If two applications both try reading a notes.txt file via openFileInput(), they will each access their own edition of the file. If you need to have one file accessible from many places, you probably want to create a content provider, as will be described in Chapter 28.

Note that openFileInput() and openFileOutput() do not accept file paths (e.g., path/to/file.txt), just simple filenames.

The following code shows the layout for the world’s most trivial text editor, pulled from the Files/ReadWrite sample application available on the Apress Web site:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="fill_parent"

 android:layout_height="fill_parent"

 android:orientation="vertical">

 <Button android:id="@+id/close"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:text="Close" />

 <EditText

  android:id="@+id/editor"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:singleLine="false"

 />

</LinearLayout>

All we have here is a large text-editing widget with a Close button above it. The Java is only slightly more complicated:

package com.commonsware.android.files;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import java.io.BufferedReader;

import java.io.File;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

public class ReadWriteFileDemo extends Activity {

 private final static String NOTES = "notes.txt";

 private EditText editor;

 @Override

 public void onCreate(Bundle icicle) {

  super.onCreate(icicle);

  setContentView(R.layout.main);

  editor = (EditText)findViewById(R.id.editor);

  Button btn = (Button)findViewById(R.id.close);

  btn.setOnClickListener(new Button.OnClickListener() {

   public void onClick(View v) {

    finish();

   }

  });

 }

 public void onResume() {

  super.onResume();

  try {

   InputStream in = openFileInput(NOTES);

   if (in != null) {

    InputStreamReader tmp = new InputStreamReader(in);

    BufferedReader reader = new BufferedReader(tmp);

    String str;

    StringBuffer buf = new StringBuffer();

    while ((str = reader.readLine()) != null) {

     buf.append(str+"\n");

    }

    in.close();

    editor.setText(buf.toString());

   }

  } catch (java.io.FileNotFoundException e) {

   // that's OK, we probably haven't created it yet

  } catch (Throwable t) {

   Toast.makeText(this, "Exception: " + t.toString(), 2000).show();

  }

 }

 public void onPause() {

  super.onPause();

  try {

   OutputStreamWriter out =

    new OutputStreamWriter(openFileOutput(NOTES, 0));

   out.write(editor.getText().toString());

   out.close();

  } catch (Throwable t) {

   Toast.makeText(this, "Exception: " + t.toString(), 2000).show();

  }

 }

}

First we wire up the button to close out our activity when it’s clicked, by using setOnClickListener() to invoke finish() on the activity.

Next we hook into onResume() so we get control when our editor is coming to life, from a fresh launch or after having been frozen. We use openFileInput() to read in notes.txt and pour the contents into the text editor. If the file is not found, we assume this is the first time the activity was run (or that the file was deleted by other means), and we just leave the editor empty.

Finally we hook into onPause() so we get control as our activity gets hidden by another activity or is closed, such as via our Close button. Here we use openFileOutput() to open notes.txt, into which we pour the contents of the text editor.

The net result is that we have a persistent notepad: whatever is typed in will remain until deleted, surviving our activity being closed, the phone being turned off, and similar situations.

CHAPTER 19

Working with Resources

Resources are static bits of information held outside the Java source code. You have seen one type of resource — the layout — frequently in the examples in this book. There are many other types of resource, such as images and strings, that you can take advantage of in your Android applications.

The Resource Lineup

Resources are stored as files under the res/ directory in your Android project layout. With the exception of raw resources (res/raw/), all the other types of resources are parsed for you, either by the Android packaging system or by the Android system on the device or emulator. For example, when you lay out an activity’s UI via a layout resource (res/layout/), you do not have to parse the layout XML yourself — Android handles that for you.