The first Activity in FileExplorer is a screen that allows users to choose whether to work with the internal or external storage.
If the user chooses the internal storage path, we then go to an Activity named InternalStorage.
the user enters some text and clicks the Write button to store that text to a file. When they click Read, the file is read back and displayed.
create layout file 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:id="@+id/main_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10px"
android:text="Choose an option to continue:" />
<Button
android:id="@+id/main_internal_storage_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use internal storage" />
<Button android:id="@+id/main_external_storage_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use external storage" />
</LinearLayout>
create layout file
internal_storage.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:id="@+id/internal_storage_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10px"
android:text="Enter some text to write on the internal storage, then read back:" />
<EditText android:id="@+id/internal_storage_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10px" />
<Button android:id="@+id/internal_storage_write_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write" />
<Button android:id="@+id/internal_storage_read_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Read" />
<TextView android:id="@+id/internal_storage_output"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10px" />
</LinearLayout>
create main activity:
public class Main extends Activity implements OnClickListener {
private Button internalStorage;
private Button externalStorage;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.internalStorage = (Button) findViewById(R.id.main_internal_storage_button);
this.internalStorage.setOnClickListener(this);
this.externalStorage = (Button) findViewById(R.id.main_external_storage_button);
this.externalStorage.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.equals(internalStorage)){
startActivity(new Intent(this,InternalStorage.class));
}
else if(v.equals(externalStorage)){
startActivity(new Intent(this,ExternalStorage.class));
}
}
}
create
InternalStorage activity:
public class InternalStorage extends Activity {
// no utils here, just basic java.io
// also be aware of getCacheDir, which writes to an internal
//directory that the system may clean up
private static final String LINE_SEP=System.getProperty("line.separator");
private EditText input;
private TextView output;
private Button write;
private Button read;
@Override
public void onCreate(final Bundle icicle) {
super.onCreate(icicle);
this.setContentView(R.layout.internal_storage);
this.input = (EditText) findViewById(R.id.internal_storage_input);
this.output = (TextView) findViewById(R.id.internal_storage_output);
this.write = (Button) findViewById(R.id.internal_storage_write_button);
this.write.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
write();
}
});
this.read = (Button) findViewById(R.id.internal_storage_read_button);
this.read.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
read();
}
});
}
private void write(){
FileOutputStream fos=null;
try{
//obtain a FileOutputStream with openFileOutput
fos=openFileOutput("test.txt",Context.MODE_PRIVATE);
fos.write(input.getText().toString().getBytes());
Toast.makeText(this, "File writen", Toast.LENGTH_SHORT).show();
input.setText("");
output.setText("");
}catch(FileNotFoundException e){
Log.e(Constants._COUNT, "File not found", e);
}catch(IOException e){
Log.e(Constants._COUNT, "IO problem", e);
}finally{
try{
fos.close();
}catch(IOException e){
}
}
}
private void read(){
FileInputStream fis=null;
Scanner scanner=null;
StringBuilder sb=new StringBuilder();
try{
fis=openFileInput("test.txt");
// scanner does mean one more object, but it's easier to work with
scanner=new Scanner(fis);
while(scanner.hasNextLine()){
sb.append(scanner.nextLine()+LINE_SEP);
}
Toast.makeText(this, "File read", Toast.LENGTH_SHORT).show();
}catch(FileNotFoundException e){
Log.e(Constants._COUNT, "File not found", e);
}finally{
if(fis!=null){
try{
fis.close();
}catch(IOException e){
}
}
if(scanner!=null){
scanner.close();
}
}
output.setText(sb);
}
}
Reading and writing data to and from filesystem, internal or external, can block the main UI thread. In a real implementation, you’ll want to do this from a Handler or an AsyncTask (passing in the file reference).