C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Android External Storage ExampleLike internal storage, we are able to save or read data from the device external memory such as sdcard. The FileInputStream and FileOutputStream classes are used to read and write data into the file. Example of reading and writing data in the android external storageactivity_main.xmlDrag the 2 edittexts, 2 textviews and 2 buttons from the pallete, now the activity_main.xml file will like this: File: activity_main.xml
Provide permission for the external storageYou need to provide the WRITE_EXTERNAL_STORAGE permission. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> File: Activity_Manifest.xml
Activity classLet's write the code to write and read data from the android external storage. File: MainActivity.java
package example.TheDeveloperBlog.com.externalstorage; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; 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; import java.io.OutputStreamWriter; public class MainActivity extends AppCompatActivity { EditText editTextFileName,editTextData; Button saveButton,readButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextFileName=findViewById(R.id.editText1); editTextData=findViewById(R.id.editText2); saveButton=findViewById(R.id.button1); readButton=findViewById(R.id.button2); //Performing action on save button saveButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View arg0) { String filename=editTextFileName.getText().toString(); String data=editTextData.getText().toString(); FileOutputStream fos; try { File myFile = new File("/sdcard/"+filename); myFile.createNewFile(); FileOutputStream fOut = new FileOutputStream(myFile); OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); myOutWriter.append(data); myOutWriter.close(); fOut.close(); Toast.makeText(getApplicationContext(),filename + "saved",Toast.LENGTH_LONG).show(); } catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} } }); //Performing action on Read Button readButton.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View arg0) { String filename=editTextFileName.getText().toString(); StringBuffer stringBuffer = new StringBuffer(); String aDataRow = ""; String aBuffer = ""; try { File myFile = new File("/sdcard/"+filename); 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(); } Toast.makeText(getApplicationContext(),aBuffer,Toast.LENGTH_LONG).show(); } }); } } |