
What is External Storage ?
External Storage such as a SD Card to store data.it is no security enforced files you save to the external storage. External Storage to save,read and write data from externally storage such as a SD Card.
External Storage are FileInputStream and FileOutputStream classes are used to read and write data into the file.
External Storage are two type of storage.
Primary External Storage: Primary Storage is a built in shared storage.it is accessible by user by plugging in a USB cable and mounting it as a drive on a host computer.
Secondary External Storage : Secondary Storage is a Removable storage Like SD Card.
All the Application read and write file from placed on external storage.when user remove external storage to first check external storage are available or not.user to remove external storage to does not read and write file in external storage.when external storage is available to read and write files.
External Storage Required Permission in AndroidManifest.xml File copy this code and paste AndroidManifest.xml file.
1 2 |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> |
Above the permission is granted on your application to access external storage is read and write functionality otherwise it does not read and write external storage on your application.
How to Store File from External Storage ?
Create activity_main.xml file Following this code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_external_storage" 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" android:orientation="vertical" tools:context="com.bhaumik.MainActivity"> <EditText android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:layout_marginTop="10dp" android:hint="Enter a Message" android:gravity="top" android:textSize="18dp" android:id="@+id/et_message"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Write Message" android:id="@+id/btn_write" android:textAllCaps="false" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Read Message" android:textAllCaps="false" android:id="@+id/btn_read"/> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:layout_marginTop="10dp" android:hint="Show Message" android:textSize="18dp" android:id="@+id/tv_show"/> </LinearLayout> |
Create MainActivity.java File Following below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
package com.bhaumik; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { EditText editText; TextView textView; Button write,read; String file_name = "test.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) findViewById(R.id.et_message); textView = (TextView) findViewById(R.id.tv_show); write = (Button) findViewById(R.id.btn_write); read = (Button) findViewById(R.id.btn_read); textView.setVisibility(View.GONE); write.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Check External Storage is Available or Not on the Device String states = Environment.getExternalStorageState(); if(Environment.MEDIA_MOUNTED.equals(states)) { // External Storage Get Root File root = Environment.getExternalStorageDirectory(); //Create Folder/Directory on the External Storage File dir = new File(root.getAbsolutePath() + "/MyFiles"); //Check Folder/Directory is Exists or Not if(!dir.exists()) { dir.mkdir(); } File file = new File(dir,file_name); String message = editText.getText().toString(); try { FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(message.getBytes()); fileOutputStream.close(); Toast.makeText(MainActivity.this,"Saved Success....",Toast.LENGTH_SHORT).show(); editText.setText(""); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } else { Toast.makeText(MainActivity.this,"SD Card Not Found",Toast.LENGTH_SHORT).show(); } } }); read.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // External Storage Get Root File root = Environment.getExternalStorageDirectory(); //Create Folder/Directory on the External Storage File dir = new File(root.getAbsolutePath() + "/MyFiles"); File file = new File(dir,file_name); String msg; try { FileInputStream fileInputStream = new FileInputStream(file); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuffer stringBuffer = new StringBuffer(); while ((msg = bufferedReader.readLine()) != null) { stringBuffer.append(msg + "\n"); } textView.setText(stringBuffer.toString()); textView.setVisibility(View.VISIBLE); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } } |
Above this code i have create one input edittext and two button is read and write and then one textview to display file data.when you can type your name in the edittext and then click write button to check External Storage is available or not when your external storage is available then create file root and directory.first is check directory and folder are available in external storage or not it does not available to create directory and store file and display toast.when you can press read button to display textview file and typed your message show.