External storage


External storage

Files that you choose to save in the external storage of your device are public. Anybody including the other apps in your device can review or read those files. You can modify them by permitting USB mass storage to share the files on another device.

If you have the API L version 8 or higher, the getExternalFilesDir() will help you to see a File that shows the location where you must save these files .i.e., external storage directory. Plus, this method has a special parameter that specifies the category of the subdirectory you prefer like DIRECTORY_VIDEOS and DIRECTORY_ALARMS. In this manner, you get to arrange the files, so they don’t get mixed up.

Code

The layout of activity_main.xml is given as follows:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"   android:layout_width="fill_parent" android:layout_height="fill_parent"   android:orientation="horizontal">     <TextView android:layout_width="fill_parent"    android:layout_height="wrap_content"     android:text="Reading and Writing to External Storage" android:textSize="24sp"/>       <EditText android:id="@+id/myInputText"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:ems="10" android:lines="5"     android:minLines="3" android:gravity="top|right"     android:inputType="textMultiLine">       <requestFocus />     </EditText>      <LinearLayout     android:layout_width="match_parent" android:layout_height="wrap_content"     android:orientation="vertical"     android:weightSum="1.0"     android:layout_marginTop="20dp">       <Button android:id="@+id/saveExternalStorage"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:text="SAVE"     android:layout_weight="0.5"/>       <Button android:id="@+id/getExternalStorage"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_weight="0.5"     android:text="READ" />   </LinearLayout>       <TextView android:id="@+id/response"     android:layout_width="wrap_content"     android:layout_height="wrap_content" android:padding="5dp"     android:text=""     android:textAppearance="?android:attr/textAppearanceMedium" />   </LinearLayout>

Other than saving and reading from external storage buttons, in this text view, we have shown the response of saving, reading from external storage.

For MainActivity.java class, the text view is shown below:

package com.journaldev.externalstorage;   import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import android.os.Bundle; import android.app.Activity; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;   public class MainActivity extends Activity { EditText inputText; TextView response; Button saveButton,readButton;       private String filename = "SampleFile.txt";     private String filepath = "MyFileStorage"; File myExternalFile; String myData = ""; }

If you're using API 7th level or lower, then getExternalStorageDirectory() will do the job for you. It will open the file that denotes the origin of the external storage.

Image

Image Not Found