0% found this document useful (0 votes)
83 views5 pages

SDcard

The document describes an Android application that reads from and writes to an SD card. It includes the XML layout file (MainActivity.xml) which contains edit texts, buttons to read, write and clear data. The Java code (MainActivity.java) handles the button clicks to read data from a file on the SD card into an edit text, write the edit text data to a file on the SD card, and clear the edit text.

Uploaded by

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

SDcard

The document describes an Android application that reads from and writes to an SD card. It includes the XML layout file (MainActivity.xml) which contains edit texts, buttons to read, write and clear data. The Java code (MainActivity.java) handles the button clicks to read data from a file on the SD card into an edit text, write the edit text data to a file on the SD card, and clear the edit text.

Uploaded by

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

Sdcard

Design:
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.surya3.sdcard.MainActivity">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="449dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/linearLayout">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtdata" />
</LinearLayout>

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/linearLayout"
android:layout_alignEnd="@+id/linearLayout"
android:layout_marginTop="41dp">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="READ"
android:id="@+id/button"
android:onClick="readdata"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WRITE"
android:id="@+id/button2"
android:onClick="writedata"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLEAR"
android:id="@+id/button3"
android:onClick="cleardata"/>
</LinearLayout>
</RelativeLayout>
MainActivity.java
package com.example.surya3.sdcard;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
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;
import java.io.OutputStreamWriter;

public class MainActivity extends ActionBarActivity {


EditText E1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
E1=(EditText)findViewById(R.id.txtdata);

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
public void readdata(View view){
try{
File f=new File("/sdcard/myfile.txt");
FileInputStream fin=new FileInputStream(f);
BufferedReader bf=new BufferedReader(new
InputStreamReader(fin));
String drow="";
String dbuf="";
while ((drow=bf.readLine())!=null){
dbuf+=drow+"\n";
}
E1.setText(dbuf);
bf.close();
fin.close();
}catch (Exception e)
{
Toast.makeText(getBaseContext(),"Data read from SD
Card",Toast.LENGTH_LONG).show();
}
}
public void writedata(View view){
try {
File f = new File("/sdcard/myfile.txt");
f.createNewFile();
FileOutputStream fout = new FileOutputStream(f);
OutputStreamWriter mout = new OutputStreamWriter(fout);
mout.append(E1.getText().toString());
mout.close();
fout.close();
Toast.makeText(getBaseContext(), "Data Written on SD card",
Toast.LENGTH_LONG).show();
}
catch (Exception e)
{

Toast.makeText(getBaseContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
}
public void clear(View view){
E1.setText("");
}
}
Output:

You might also like