Sunday, October 16, 2011

Read file from Internal Storage via FileInputStream

The file in Internal Storage can be read using read() method of FileInputStream class.

Further work on last exercise "List the saved files in Internal Storage"; once any item in the ListView of saved files is clicked, the file will be opened using FileInputStream.read(), and the content will be displayed in a custom dialog.

Read file from Internal Storage via FileInputStream

package com.exercise.AndroidInternalStorage;

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

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
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();

}});

listSavedFiles.setOnItemClickListener(listSavedFilesOnItemClickListener);

}

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

listSavedFiles.setAdapter(adapter);
}

OnItemClickListener listSavedFilesOnItemClickListener
= new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String clickedFile = (String) parent.getItemAtPosition(position);
OpenFileDialog(clickedFile);
}

};

void OpenFileDialog(String file){

//Read file in Internal Storage
FileInputStream fis;
String content = "";
try {
fis = openFileInput(file);
byte[] input = new byte[fis.available()];
while (fis.read(input) != -1) {}
content += new String(input);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

//Create a custom Dialog
AlertDialog.Builder fileDialog
= new AlertDialog.Builder(AndroidInternalStorageActivity.this);
fileDialog.setTitle(file);

TextView textContent = new TextView(AndroidInternalStorageActivity.this);
textContent.setText(content);
LayoutParams textViewLayoutParams
= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textContent.setLayoutParams(textViewLayoutParams);

fileDialog.setView(textContent);

fileDialog.setPositiveButton("OK", null);

fileDialog.show();
}
}


Download the files.

next:
- Delete file in Internal Storage



2 comments:

Anonymous said...

Thanks. U saved my day. How to read the film is been helpfull.

Unknown said...

So funny but this is truly the only version I've seen online where someone shows how to save, show, and display internal storage files! Thank you and I hope you are enjoying the simulation.