Course Content

Course Content

External Storage Example

Android External Storage Example

Till now we have seen that we can store our data in different storages such as external storage or internal storage, now we will look that how we can write in a file which is in external storage and then how we can read it. Let's understand the detailed process of the Android External Storage Example.

Stepwise :

Step-1 So for that we will first create a project named 'External Storage'. Now we will look that how we can design it.

Step-2 So very first we will look at our manifest file which is inside the 'App' folder. The manifest file 'AndroidManifest.xml' which we will open is now default generated. As we want to create our file in external storage, so for that we will need to specify some permission here in our manifest file,

<user-permission android:name=""android.permission.WRITE_EXTERNAL_STORAGE""/>

Step-3 Now we will look at the layout, so for that, we will open our 'activity_main.xml' file from the layout folder, and in that, as per requirement, we have taken two editText (editText1 and editText2), two textView (textView1 and textView2) and two-button (button1 and button2). Now we will basic properties like height, width, the margin in all of them, and have given ID to all of them because we will need that ID in our main code.

Step-4 Now we will see the main file 'MainActivity.java' which is stored inside the java folder, the activity which we will be seeing is by default generated. So now the first step will be view identification for edit texts and buttons.

Step-5 Therefore with the help of 'findViewById', we had done view identification for edit texts and buttons.

Step-6 So now next step will be to generate the code for external storage for click buttons which we are using, so for that, we will first design ClickListener for the 'Save' button,

saveButton.setOnClickListener(new OnClickListener);

Step-7 Now we will design code for save button that how on click we will save the file in external storage and read the data. Now we will create two strings for two editText and with the help of gettext() we will proceed.

Step-8 Now an instance of 'FileOutputStream' will be used to write the data from external storage, FileOutputStream fos;

Step-9 Now to generate code we will need to create a file with instance,

File myfile=new File (""/sdcard""+filename);

Step-10 Now to generate code we will need to create a file with instance,

File myFile=new File (""/sdcard""+filename);

Now we need to create file,

myFile.createNewFile();

Now we will pass the instance of FileOutputStream,

fos=new FileOutputStream (myFile);

Step-11 After that we will use 'OutputStreamWriter' to write the content inside our file,

OutputStreamWriter myOutWriter=new OutputStreamWriter(fos);

Step-12 Now we are appending the data which is being added by the user to that file,

myOutWriter.append(data);

myOutWriter.close();

fos.close();

Step-13 Now we will use a 'Toast notification' to show that the data which user is adding is being added or not,

Toast.makeText(getApplicationContext(),filename+""saved"",Toast.LENGTH_LONG).show();

Step-14 Now we will design a Listener for 'read' button,

readButton.setOnClickListener(new OnClickListener());

Step-15 Now we will hear use 'StringBuffer' because we want to read data this time,

StringBuffer stringbuffer=new StringBuffer();

Step-16 Then we have taken two different blank string,

String aDataRow="" "";

String aBuffer="" "";

Step-17 Now using ‘FileInputStream’ we will read the data,

FileInputStream fIn=new FileInputStream(myFile);

Step-18 Now we will use BufferReader,

BufferReader bufferreader=new BufferReader(new InputStreamReader(fIn));

Step-19 Now on Condition basis to read file we will write down a code,

while {(aDataRow=myReader.readLine()} != null)

{

aBuffer=aDataRow+""\\n"";

}

myReader.close();

}                             

catch (IOException e)

{

e.printStackTrace();

}

Toast.makeText(getApplicationContext(), aBuffer, Toast.LENGTH_LONG). show();

}

Step-20 Now we will execute it and see how it is working. As we run our code it is working absolutely perfect, and the data is displayed very well as per our coding.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download LearnVern App

App Preview Image
App QR Code Image
Code Scan or Download the app
Google Play Store
297K+ Downloads
App Download Section Circle 1
4.5 Rating
App Download Section Circle 2
10K+ Reviews
App Download Section Circle 3
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere