Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
97 views7 pages

EXNO10-Writes Data To The SD Card

The document describes an algorithm for implementing an Android application to write data to an SD card. The application creates a new Android project and adds buttons and text fields to the main layout file. It then writes user-entered text to a file on the SD card when a button is clicked, and can also read the file and display the text. The application requires the WRITE_EXTERNAL_STORAGE permission to access the SD card.

Uploaded by

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

EXNO10-Writes Data To The SD Card

The document describes an algorithm for implementing an Android application to write data to an SD card. The application creates a new Android project and adds buttons and text fields to the main layout file. It then writes user-entered text to a file on the SD card when a button is clicked, and can also read the file and display the text. The application requires the WRITE_EXTERNAL_STORAGE permission to access the SD card.

Uploaded by

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

EX NO: 10 WRITES DATA TO THE SD CARD

DATE:

AIM:
To Implement an application that writes data to the SD card.

ALGORITHM:
1)Open eclipse or android studio and select new android project
2)Give project name and select next
3) Choose the android version.Choose the lowest android version(Android 2.2) and select next
4) Enter the package name.package name must be two word separated by comma and click finish
5)Go to package explorer in the left hand side.select our project.
6)Go to res folder and select layout.Double click the main.xml file. Add the code below
<?xml version=”1.0” encoding=”utf-8”?>
<LinearLayout
android:id=”@+id/widget28”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:background=”#ff0000ff”
android:orientation=”vertical”
xmlns:android=”http://schemas.android.com/apk/res/android”
>
<EditText
android:id=”@+id/txtData”
android:layout_width=”fill_parent”
android:layout_height=”180px”
android:textSize=”18sp” />

<Button
android:id=”@+id/btnWriteSDFile”
android:layout_width=”143px”
android:layout_height=”44px”
android:text=”1. Write SD File” />

<Button
android:id=”@+id/btnClearScreen”
android:layout_width=”141px”
android:layout_height=”42px”
android:text=”2. Clear Screen” />

<Button
android:id=”@+id/btnReadSDFile”
android:layout_width=”140px”
android:layout_height=”42px”
android:text=”3. Read SD File” />

<Button
android:id=”@+id/btnClose”
android:layout_width=”141px”
android:layout_height=”43px”
android:text=”4. Close” />

</LinearLayout>
7) Now select FileDemo2.java file and type the following code.
Package com.javasamples;
import java.io.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class FileDemo2 extends Activity {


// GUI controls
EditText txtData;
Button btnWriteSDFile;
Button btnReadSDFile;
Button btnClearScreen;
Button btnClose;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// bind GUI elements with local controls
txtData = (EditText) findViewById(R.id.txtData);
txtData.setHint(“Enter some lines of data here…”);

btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);


btnWriteSDFile.setOnClickListener(new OnClickListener() {

public void onClick(View v) {


// write on SD card file data in the text box
try {
File myFile = new File(“/sdcard/mysdfile.txt”);
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(txtData.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
“Done writing SD ‘mysdfile.txt’”,
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}// onClick
}); // btnWriteSDFile

btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile);


btnReadSDFile.setOnClickListener(new OnClickListener() {

public void onClick(View v) {


// write on SD card file data in the text box
try {
File myFile = new File(“/sdcard/mysdfile.txt”);
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = “”;
String aBuffer = “”;
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + “\n”;
}
txtData.setText(aBuffer);
myReader.close();
Toast.makeText(getBaseContext(),
“Done reading SD ‘mysdfile.txt’”,
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}// onClick
}); // btnReadSDFile
btnClearScreen = (Button) findViewById(R.id.btnClearScreen);
btnClearScreen.setOnClickListener(new OnClickListener() {

public void onClick(View v) {


// clear text box
txtData.setText(“”);
}
}); // btnClearScreen

btnClose = (Button) findViewById(R.id.btnClose);


btnClose.setOnClickListener(new OnClickListener() {

public void onClick(View v) {


// clear text box
finish();
}
}); // btnClose

}// onCreate

}// AndSDcard
8)Next step is to set permission to write data in sd card.So go to AndroidManifest.xml file. Copy and
paste the following coding.The code should come before <application> tab.

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

9)Now go to main.xml and right click .select run as option and select run configuration
10) Android output is present in the android emulator as shown in below.
OUTPUT
RESULT:

Thus the application that writes data to the SD card was executed successfully.

You might also like