Android File Demonstration

We tested the code by compiling for both a Nexus emulator and an AndroBOX virtual machine and confirmed that saved text was retrieved when the app was reopened in the current session and when it was opened after restarting each simulator. The screenshot shows a saved "to do" list.

Screenshot

Screenshot

Please inform us if you manage to simplify the code without losing any of its functionality.

We adapted convertStreamToString from existing Java code.

The code of MainActivity.pas

namespace org.me.file_demo;

interface

uses
  java.util, android.app, android.content, android.os, android.util, android.view, android.widget, java.io;

type
  MainActivity = public class(Activity)
  private
    myButton : Button;
    edtToDo :EditText;
  public
    method onCreate(savedInstanceState: Bundle); override;
    method ButtonOnClick(v: View);
    method onResume; override;
    class method convertStreamToString(ips : java.io.InputStream) : String;
  end; 

implementation

method MainActivity.onCreate(savedInstanceState: Bundle);
var
  inputStream : FileInputStream;
  filename : String := 'ToDo.txt';
  msg : array[0 .. 1000] of Byte;
  num : Integer;
begin
  inherited;
  ContentView := R.layout.main;
  myButton := Button(findViewById(R.id.MyButton));
  edtToDo  := EditText(findViewById(R.id.edt));
  myButton.OnClickListener := new interface View.OnClickListener(onClick := @ButtonOnClick);
  var f : File  := new File(filename);
  if f.exists then
    begin
      try 
        inputStream := openFileInput(filename);
        num := inputStream.read(msg);
        inputStream.close;
      except
        on e : Exception do
        e.printStackTrace;
      end;
      edtToDo.setText(msg.toString); 
   end;
 end;

class method MainActivity.convertStreamToString(ips : java.io.InputStream) : String;
begin
  var s : java.util.Scanner  := new java.util.Scanner(ips).useDelimiter("\\A");
  if s.hasNext() then
    result := s.next 
  else
    result := '';
end;

method MainActivity.onResume; 
var
  inputStream : FileInputStream;
  filename : String := 'ToDo.txt';
  myString : String;
begin
  inherited;
  try 
    inputStream := openFileInput(filename);
    myString  := convertStreamToString(inputStream);
    edtToDo.setText(myString);
    inputStream.close;
  except
    on e : Exception do
      e.printStackTrace;
  end;
end;

method MainActivity.ButtonOnClick(v: View);
var
  outputStream : FileOutputStream;
  filename : String := 'ToDo.txt';
begin
  try 
    outputStream := openFileOutput(filename, Context.MODE_PRIVATE);
    outputStream.write(edtToDo.getText.toString.getBytes);
    outputStream.close;
  except
   on e : Exception do
     e.printStackTrace;
  end;  
end;

end.

Layout File

<?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"
    android:gravity="center_vertical|center_horizontal">
    <Button
      android:id="@+id/MyButton"
      android:text="@string/my_button_text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  </LinearLayout>

Strings File

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">File Demo</string>
  <string name="my_button_text">Save</string>
  <string name="edt_text">TO DO</string>
</resources>
Programming - a skill for life!

How to use EditText, Toast, DatePicker, ListView, RadioGroup, ScrollView and ImageView widgets, sound and files in Oxygene for Java Android apps