Android - SPLessons

Android Internal External Storage

Home > Lesson > Chapter 19
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android Internal External Storage

Android Internal External Storage

Android provides various storage options for applications to store data. You can store data on shared preferences, internal and external storage, SQLite storage, and storage via network connection. Android allows you to store files in its file system, which is very similar to any other Linux file system. You can read and write files to the Android file system using the java.io file input/output APIs. This is useful when you want to store files (key/value cache pairs) on the device. Files like audio, video, images, documents, etc. can be stored in the file system as on when required. In this chapter, we are going to explain you about how to store data on both internal as well as external storage(sdcard). Internal Storage: Any private data can be stored in Internal storage. The application will always have access to the data, which gets deleted when the user uninstall the app. After creating the file in the internal storage, it is stored into dada/data/package name/file or cache directory. Let's see how to do this: open DDMS->File Explorer. Ex: Create MainActivity.java under src/<your packagename> MainActivity.java: [java] public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button save=(Button)findViewById(R.id.button1); Button show=(Button)findViewById(R.id.button2); final EditText text=(EditText)findViewById(R.id.editText1); final TextView dText=(TextView)findViewById(R.id.textView1); //on click save save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Create a file in the Internal Storage //The constant Context.MODE_PRIVATE makes the file inaccessible to other apps. /*String fileName = "MyFile"; String data = text.getText().toString(); FileOutputStream outputStream = null; try { outputStream = openFileOutput(fileName, Context.MODE_PRIVATE); outputStream.write(content.getBytes()); outputStream.close(); } catch (Exception e) { e.printStackTrace(); }*/ /*create cache files rather than storing data in files persistently. We can open a File object representing the cache directory in the internal storage by using the getCacheDir() method.*/ String data = text.getText().toString(); File file = null; FileOutputStream outputStream; try { file = new File(getCacheDir(), "MyFile"); outputStream = new FileOutputStream(file); outputStream.write(data.getBytes()); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }); //on click show show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub BufferedReader input = null; File file = null; try { // Pass getFilesDir() and "MyFile" to read file file = new File(getCacheDir(), "MyFile"); input = new BufferedReader(new InputStreamReader(new FileInputStream(file))); String line; StringBuffer buffer = new StringBuffer(); while ((line = input.readLine()) != null) { buffer.append(line); dText.setText(line); } } catch (IOException e) { e.printStackTrace(); } } }); } } [/java] Create activity_main.xml under res/layout folder activity_main.xml: [code lang="xml"] <button> </button> <button> </button> [/code] AndroidManifest.xml: [code lang="xml"] [/code] To delete files use file.delete(); Output: External storage: External storage can be either removable like an SD card or non-removable if it is internal. Files in this storage are in readable mode, which means that the other applications can access these files. Before writing into these files, you should make sure that they are available, as they can become unavailable if the SD card is removed or unmounted from the user's device. In this case, the file gets stored to mnt/sdcard directory. Let's see how to do this: open DDMS->File Explorer. Ex: Create MainActivity.java under src/<your packagename> MainActivity.java: [java] public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button save=(Button)findViewById(R.id.button1); Button show=(Button)findViewById(R.id.button2); final EditText text=(EditText)findViewById(R.id.editText1); final TextView dText=(TextView)findViewById(R.id.textView1); //on click save save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String content = text.getText().toString(); File file; FileOutputStream outputStream; try { file = new File(Environment.getExternalStorageDirectory(), "MyFile"); outputStream = new FileOutputStream(file); outputStream.write(content.getBytes()); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }); //on click show show.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String aDataRow = ""; String aBuffer = ""; try { File myFile = new File(Environment.getExternalStorageDirectory(), "MyFile"); FileInputStream fIn = new FileInputStream(myFile); BufferedReader myReader = new BufferedReader( new InputStreamReader(fIn)); while ((aDataRow = myReader.readLine()) != null) { aBuffer += aDataRow + "\n"; } myReader.close(); } catch (IOException e) { e.printStackTrace(); } dText.setText(aBuffer); } }); } } [/java] Create activity_main.xml under res/layout folder activity_main.xml: [code lang="xml"] <button> </button> <button> </button> [/code] AndroidManifest.xml: You need to add the following permissions to your AndroidManifest.xml file.
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
[code lang="xml"] [/code] Output: