Download this example here : InernalStorage.zip
Screen Shots:
you can check file creation in DDMS Perspective as follows:
Screen Shots:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="enter file name:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText_filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="enter text to save"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/button_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Create file and save text" />
<Button
android:id="@+id/button_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="view text from saved file" />
<TextView
android:id="@+id/textView_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="enter file name:"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText_filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="enter text to save"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/editText_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/button_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="Create file and save text" />
<Button
android:id="@+id/button_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:text="view text from saved file" />
<TextView
android:id="@+id/textView_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MainActivity.java:
package com.ram.internalstorage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText editFile, editText;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editFile = (EditText) findViewById(R.id.editText_filename);
editText = (EditText) findViewById(R.id.editText_text);
text = (TextView) findViewById(R.id.textView_display);
Button saveBtn = (Button) findViewById(R.id.button_save);
Button viewBtn = (Button) findViewById(R.id.button_view);
saveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String fileName = editFile.getText().toString();
String textToSave = editText.getText().toString();
try {
FileOutputStream fos = openFileOutput(fileName + ".txt",
MODE_PRIVATE);
fos.write(textToSave.getBytes());
fos.close();
Toast.makeText(getApplicationContext(),
"file created and text 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();
}
}
});
viewBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
FileInputStream fis = openFileInput(editFile.getText()
.toString() + ".txt");
StringBuffer sb = new StringBuffer();
int i;
while ((i = fis.read()) != -1) {
sb.append((char) i);
}
fis.close();
text.setText(sb.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText editFile, editText;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editFile = (EditText) findViewById(R.id.editText_filename);
editText = (EditText) findViewById(R.id.editText_text);
text = (TextView) findViewById(R.id.textView_display);
Button saveBtn = (Button) findViewById(R.id.button_save);
Button viewBtn = (Button) findViewById(R.id.button_view);
saveBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String fileName = editFile.getText().toString();
String textToSave = editText.getText().toString();
try {
FileOutputStream fos = openFileOutput(fileName + ".txt",
MODE_PRIVATE);
fos.write(textToSave.getBytes());
fos.close();
Toast.makeText(getApplicationContext(),
"file created and text 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();
}
}
});
viewBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
FileInputStream fis = openFileInput(editFile.getText()
.toString() + ".txt");
StringBuffer sb = new StringBuffer();
int i;
while ((i = fis.read()) != -1) {
sb.append((char) i);
}
fis.close();
text.setText(sb.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
No comments:
Post a Comment