What are the different storage options available in Android? Explain internal storage.
Android provides several storage options for data. Following are the important storage options for android app.
- Shared Preferences
- Internal Storage
- External Storage
- SQLite Database
- Internal storage is best when you want to be sure that neither the anonymous user nor other apps can access your files.
- Internal storage enables you to read and write data into files that are associated with each application’s internal memory. These files can only be accessed by the application and cannot be accessed by other applications or users.
- When the application is uninstalled, these files are automatically removed from device.
- Other apps and users cannot browse your internal directories and do not have read and write permission unless you explicitly set the files to be readable or writable.
- When you use MODE-PRIVATE for your files on the internal storage, they are never accessible to other apps.
Other modes are- MODE_APPEND
- MODE_WORLD_READABLE
- MODE_WORLD_WRITEABLE
Writing data into file:
For using internal storage to write data in the file, call the
openFileOutput() method. This method takes two parameters. First parameter is the name of the file and second is file mode.
FileOutputStreamfopStream = openFileOutput("file name here", MODE_PRIVATE);
- File name should be without path and MODE_PRIVATE will create the file or replace a file of the same name if it already exists.
- openFileOutput()method returns FileOutputStream object. After that you can call write method to write data into the file as given blow code.
String str = "CareerRide;
fopStream.write(str.getBytes());
fopStream.close();
Reading data from file:
For reading data from file, call the
openFileInput() method with the name of the file. openFileInput() method takes one parameter as file name. This name should be same as you have written in
openFileOutput method. It returns an instance of FileInputStream class.
int read = -1;
StringBuffersb = newStringBuffer();
FileInputStreamfipStream = openFileInput("file name here");
while((read = fipStream.read()) ! = -1)
{
sb.append((char)read);
}
How to write data into internal storage/ file? Give example.
Writing data into file example
Following is the content of res/layout/activity_main.xml file:
<RelativeLayoutxmlns: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:paddingBottom = "@dimen/activity_vertical_margin"
android:paddingLeft = "@dimen/activity_horizontal_margin"
android:paddingRight = "@dimen/activity_horizontal_margin"
android:paddingTop = "@dimen/activity_vertical_margin"
tools:context = ".MainActivity">
<Button
android:id = "@+id/btnNext"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignBottom = "@+id/btnSave"
android:layout_marginLeft = "35dp"
android:layout_toRightOf = "@+id/btnSave"
android:text = "Next Activity"
android:onClick = "next"
/>
<TextView
android:id = "@+id/textView2"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignLeft = "@+id/textView1"
android:layout_below = "@+id/txtUserName"
android:layout_marginTop = "70dp"
android:text = "Password"
android:textSize = "15dp"
/>
<EditText
android:id = "@+id/txtPassword"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignBottom = "@+id/textView2"
android:layout_toRightOf = "@+id/textView1"
android:ems = "10"
android:inputType = "textPassword">
</EditText>
<Button
android:id = "@+id/btnSave"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_below = "@+id/textView2"
android:layout_marginTop = "96dp"
android:layout_toLeftOf = "@+id/editText1"
android:text = "Save"
android:onClick = "save"
/>
<TextView
android:id = "@+id/textView1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignLeft = "@+id/btnSave"
android:layout_alignParentTop = "true"
android:layout_marginTop = "30dp"
android:text = "UserName"
android:textSize = "15dp"/>
<EditText
android:id="@+id/txtUserName"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignBottom = "@+id/textView1"
android:layout_alignLeft = "@+id/txtPassword"
android:ems = "10"/>
<requestFocus/>
</RelativeLayout>
Following is the content of the modified MainActivity.java file
publicclassMainActivityextends Activity
{
EditTextname,password;
FileOutputStreamfopStream;
File file = null;
@Override
protectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText)findViewById(R.id.txtUserName);
password = (EditText)findViewById(R.id.txtPassword);
}
publicvoid save(View v)
{
String txt1 = name.getText().toString();
String txt2 = password.getText().toString();
txt1 = txt1+" ";
try
{
file = getFilesDir();
fopStream = openFileOutput ("data.txt",Context.MODE_PRIVATE);
fopStream.write(txt1.getBytes ());
fopStream.write(txt2.getBytes ());
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
fopStream.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Toast.makeText(getApplicationContext(), "Data Stored Successfully at location "+file, Toast.LENGTH_LONG).show();
}
publicvoid next(View v)
{
Toast.makeText(getApplicationContext(), "Next Activity called", Toast.LENGTH_LONG).show();
Intent intent = newIntent(getApplicationContext(), SecondActivity.class);
startActivity(intent);
}
}
Output
Writing data into file example:
Following is the content of res/layout/activity_main.xml file:
<RelativeLayoutxmlns: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:paddingBottom = "@dimen/activity_vertical_margin"
android:paddingLeft = "@dimen/activity_horizontal_margin"
android:paddingRight = "@dimen/activity_horizontal_margin"
android:paddingTop = "@dimen/activity_vertical_margin"
tools:context = ".SecondActivity">
<TextView
android:id = "@+id/textView2"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignLeft = "@+id/textView1"
android:layout_below = "@+id/txtUserName"
android:layout_marginTop = "70dp"
android:text = "Password"
android:textSize = "15dp"
/>
<EditText
android:id = "@+id/txtPassword"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignBottom = "@+id/textView2"
android:layout_toRightOf = "@+id/textView1"
android:ems = "10">
<requestFocus/>
</EditText>
<Button
android:id = "@+id/btnLoad"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_below = "@+id/textView2"
android:layout_marginTop = "96dp"
android:layout_toLeftOf = "@+id/editText1"
android:text = "Load Data"
android:onClick = "loadData"
/>
<TextView
android:id = "@+id/textView1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignLeft = "@+id/btnSave"
android:layout_alignParentTop = "true"
android:layout_marginTop = "30dp"
android:text = "UserName"
android:textSize = "15dp"/>
<EditText
android:id = "@+id/txtUserName"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignBottom = "@+id/textView1"
android:layout_alignLeft = "@+id/txtPassword"
android:ems = "10"/>
<Button
android:id = "@+id/btnPrevious"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_alignBaseline = "@+id/btnLoad"
android:layout_alignBottom = "@+id/btnLoad"
android:layout_alignRight = "@+id/txtPassword"
android:layout_marginRight = "19dp"
android:text = "Previous"
android:onClick = "previous"
/>
</RelativeLayout>
Following is the content of the modified MainActivity.java file
publicclassSecondActivityextends Activity
{
EditTextname,password;
FileOutputStreamfopStream;
File file = null;
@Override
protectedvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
name = (EditText)findViewById(R.id.txtUserName);
password = (EditText)findViewById(R.id.txtPassword);
}
publicvoidloadData(View v)
{
int read=-1;
try
{
StringBuffersb = newStringBuffer();
FileInputStreamfipStream = openFileInput("data.txt");
while((read = fipStream.read ())!=-1)
{
sb.append((char)read);
}
String text1 = sb.substring(0, sb.indexOf(" "));
String text2 = sb.substring(sb.indexOf(" ")+1);
name.setText(text1);
password.setText(text2);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
publicvoid previous(View v)
{
Toast.makeText(getApplicationContext(), "Previous Activity called", Toast.LENGTH_LONG).show();
Intent intent = newIntent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
}
Output