0% found this document useful (0 votes)
190 views8 pages

Ex - No:9 Implement An Application That Writes Data To The SD Card

The document describes how to create an Android application that writes data to an SD card. It includes steps to set up the project structure in Android Studio, define the user interface layout with buttons and text fields, and write code to handle button clicks to write, read, and clear text from a file saved on the SD card. The code provided implements these features to successfully build an app that allows writing, reading, and clearing data from an external SD card on an Android device.

Uploaded by

Ashish Kulhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
190 views8 pages

Ex - No:9 Implement An Application That Writes Data To The SD Card

The document describes how to create an Android application that writes data to an SD card. It includes steps to set up the project structure in Android Studio, define the user interface layout with buttons and text fields, and write code to handle button clicks to write, read, and clear text from a file saved on the SD card. The code provided implements these features to successfully build an app that allows writing, reading, and clearing data from an external SD card on an Android device.

Uploaded by

Ashish Kulhari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Ex.

No:9
Implement an application that writes data to the SD card.

Aim:

To develop a Android Application that writes data to the SD


Card.

Procedure:

 Open eclipse or android studio and create new project


 Select our project in the project explorer
 Go to res folder and select layout Double click the main xml file
 Type the code for main.xml or drag and drop various
components used in our program
 Drag and drop relative layout and change its properties
 Drag and drop image view and change its properties according
to our programs
 Screen layout can be viewed by clicking graphics layout tab
 Include necessary files
 Override OnCreate() function
 Create Image view and initialize its using id of some
components used in the xml program
 Save the program
 Run the program
 Output can be viewed in the android emulator
Code for Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical"
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30dp" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Write Data"
android:textSize="30dp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Read data"
android:textSize="30dp" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Clear"
android:textSize="30dp" />
</LinearLayout>

Code for AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>


<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.exno9" >
<uses-permission android:name="android.permission.INTERNET
"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Code for MainActivity.java:

package com.example.exno9;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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.FileOutputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity
{
EditText e1;
Button write,read,clear;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1= (EditText) findViewById(R.id.editText);
write= (Button) findViewById(R.id.button);
read= (Button) findViewById(R.id.button2);
clear= (Button) findViewById(R.id.button3);
write.setOnClickListener(new View.OnClickListener()
{@Override
public void onClick(View v)
{
String message=e1.getText().toString();
try{
File f=new File("/sdcard/myfile.txt");
f.createNewFile();
FileOutputStream fout=new FileOutputStream(f);
fout.write(message.getBytes());
fout.close();
Toast.makeText(getBaseContext(),"Data Written in
SDCARD",Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(),e.getMessage(),Toast.
LENGTH_LONG).show();
}
}
});
read.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String message;
String buf = "";
try
{
File f = new File("/sdcard/myfile.txt");
FileInputStream fin = new FileInputStream(f);
BufferedReader br = new BufferedReader(new
InputStreamReader(fin));
while ((message = br.readLine()) != null)
{
buf += message;
}
e1.setText(buf);
br.close();
fin.close();
Toast.makeText(getBaseContext(),"Data Recived from
SDCARD",Toast.LENGTH_LONG).show();
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_LONG).show();
}
} });
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v{
e1.setText(""); }
});
}}

Output:
Result:

Thus Android Application that writes data to the SD Card is


developed and executed successfully.

You might also like