In my last article, we learn about XmlPullParser in android.  Now we see how to use Android Persistence with Internal Storage

Android provides several options for you to save persistent application data. The solution you choose depends on your specific needs, such as whether the data should be private to your application or accessible to other applications (and the user) and how much space your data requires.

Your data storage options are the following:

1.       Internal Storage

2.       Shared Preferences

3.       External Storage

4.       SQLite Databases

5.       Network Connection 


Here, I am using Android Persistence Internal Storage. 

1.      Create an Android project.

2.      Add controls in activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    android:paddingTop="30sp"
    tools:context=".MainActivity" >

    <EditText
          android:id="@+id/myEditText"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_marginLeft="10sp"
          android:layout_marginRight="10sp"
          android:layout_marginTop="10sp"
          android:hint="Enter text" />

    <Button
          android:id="@+id/mybtn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_marginTop="20sp"
          android:text=" Save " />

    <Button
          android:id="@+id/btngetData"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:layout_marginTop="20sp"
          android:text="Retrieve Data" />

  </LinearLayout> 

3. Now add following code in

MainActivity.
 package  com.example.androiddemopersistenceexample;

 
  import java.io.BufferedReader;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
 
  import android.os.Bundle;
  import android.app.Activity;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;
 
  public class MainActivity extends Activity {
     private final static String STORETEXT = "Sample.txt";
     private EditText edittext;
     Button btnSave, btngetData;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
 
           setContentView(R.layout.activity_main);
 
           edittext = (EditText) findViewById(R.id.myEditText);
           btnSave = (Button) findViewById(R.id.mybtn);
           btngetData = (Button) findViewById(R.id.btngetData);
 
           // This method retrieve data from file and fill value in edit text
           readDatafromFile();
 
           // save button click listener
           btnSave.setOnClickListener(new OnClickListener() {
 
                @Override
                public void onClick(View v) {
                     // TODO Auto-generated method stub
                     try {
 
                           // Create file name if file not exist
                           OutputStreamWriter outwriter = new OutputStreamWriter(
                                     openFileOutput(STORETEXT, 0));
 
                           // Writes the character oneChar to this writer.
                          outwriter.write(edittext.getText().toString());
 
                           // Closes this writer.
                           outwriter.close();
 
                           // show toast message after save record
                          Toast.makeText(getApplicationContext(), "Saved data",
                                     Toast.LENGTH_SHORT).show();
 
                           // after add data clear edittext
                           edittext.setText("");
 
                     } catch (Exception e) {
 
                           // if any problem to save data or exception
                          Toast.makeText(getApplicationContext(),
                                     "Data could not save", Toast.LENGTH_SHORT).show();
                     }
                }
           });
           // Retrieve button click listener
           btngetData.setOnClickListener(new OnClickListener() {
 
                @Override
                public void onClick(View v) {
 
                     readDatafromFile();
                }
           });
     }
 
     public void readDatafromFile() {
           try {
                // A readable source of bytes.
                InputStream inputStream = openFileInput(STORETEXT);
 
                if (inputStream != null) {
 
                     // Data read from the source input stream is converted into
                     // characters by either a default or a provided character
                     // converter
                     InputStreamReader inputStreamReader = new InputStreamReader(
                                inputStream);
                     // BufferedReader wraps an existing Reader and buffers the
                     // input.
                     BufferedReader brReader = new BufferedReader(inputStreamReader);
 
                     String str;
 
                     // StringBuilder is a modifiable sequence of characters for use
                     // in creating strings.
                     StringBuilder buf = new StringBuilder();
 
                     while ((str = brReader.readLine()) != null) {
 
                           // append value in stringbuilder
                           buf.append(str);
 
                     }
 
                     // Closes this stream.
                     inputStream.close();
                     edittext.setText(buf.toString());
                }
           } catch (Exception ex) {
           }
 
     }} 

And run your application    


    

Close your activity and reopen this application

 

  Modified On Nov-30-2017 02:41:12 AM

Leave Comment