Sunday, 16 October 2011

List the saved files in Internal Storage

To list the saved files in Internal Storage, the method fileList() can be used. It returns an array of strings naming the private files associated with this Context's application package.

It's a example, in ShowSavedFiles() method, get the saved file name and list in ListView.

List the saved files in Internal Storage

package com.exercise.AndroidInternalStorage;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class AndroidInternalStorageActivity extends Activity {

EditText edFileName, edContent;
Button btnSave;
ListView listSavedFiles;

String[] SavedFiles;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edFileName = (EditText)findViewById(R.id.filename);
edContent = (EditText)findViewById(R.id.content);
btnSave = (Button)findViewById(R.id.save);
listSavedFiles = (ListView)findViewById(R.id.list);

ShowSavedFiles();

btnSave.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String fileName = edFileName.getText().toString();
String content = edContent.getText().toString();

FileOutputStream fos;
try {
fos = openFileOutput(fileName, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();

Toast.makeText(
AndroidInternalStorageActivity.this,
fileName + " saved",
Toast.LENGTH_LONG).show();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

ShowSavedFiles();

}});

}

void ShowSavedFiles(){
SavedFiles = getApplicationContext().fileList();
ArrayAdapter<String> adapter
= new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
SavedFiles);

listSavedFiles.setAdapter(adapter);
}
}


<?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/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter File Name" />
<EditText
android:id="@+id/filename"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter Content" />
<EditText
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Save"/>
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Download the files.

next:
- Read file from Internal Storage via FileInputStream



No comments:

Post a Comment