Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Implement an application that writes data to the SD card.

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 seprated 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 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ff0000ff"
    android:orientation="vertical" >

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

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SAVE DATA" />
    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="SHOW DATA" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>
7) Now select mainactivity.java file and type the following code.
package save.sd;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SavedatasdcardActivity extends Activity {
    /** Called when the activity is first created. */
    Button save,load;
    EditText message;
    TextView t1;
    String Message1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        save=(Button) findViewById(R.id.button1);
        load=(Button) findViewById(R.id.button2);
        message=(EditText) findViewById(R.id.editText1);
        t1=(TextView) findViewById(R.id.textView1);
        save.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                //Get message from user store in message1 variable
                Message1 =message.getText().toString();
                try{
                    //Create a new folder called MyDirectory in SDCard
                    File sdcard=Environment.getExternalStorageDirectory();
                    File directory=new File(sdcard.getAbsolutePath()+"/MyDirectory");             
                    directory.mkdirs();
                    //Create a new file name textfile.txt inside MyDirectory
                    File file=new File(directory,"textfile.txt");
                    //Create File Outputstream to read the file
                    FileOutputStream fou=new FileOutputStream(file);
                       OutputStreamWriter osw=new OutputStreamWriter(fou);
                    try{
                        //write a user data to file
                        osw.append(Message1);
                        osw.flush();
                        osw.close();
                        Toast.makeText(getBaseContext(),"Data Saved",Toast.LENGTH_LONG).show();
                     
                         }catch(IOException e){
                             e.printStackTrace();
                         }
                }catch (FileNotFoundException e){
                    e.printStackTrace();
                }
            }
        });
        load.setOnClickListener(new View.OnClickListener(){
                 public void onClick(View v){
                   try{
                    File sdcard=Environment.getExternalStorageDirectory();
                    File directory=new File(sdcard.getAbsolutePath()+"/MyDirectory");
                     File file=new File(directory,"textfile.txt");
                     FileInputStream fis=new FileInputStream(file);
                    InputStreamReader isr=new InputStreamReader(fis);
                    char[] data=new char[100];
                    String final_data="";
                    int size;
                    try{
                        while((size=isr.read(data))>0)
                        {
                            //read a data from file
                            String read_data=String.copyValueOf(data,0,size);
                            final_data+=read_data;
                            data=new char[100];
                        }
                        //display the data in output
                        Toast.makeText(getBaseContext(),"Message:"+final_data,Toast.LENGTH_LONG).show();
                         }catch(IOException e){
                             e.printStackTrace();
                         }
                }catch (FileNotFoundException e){
                    e.printStackTrace();
                }
            }
        });
    }
}
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.





No comments:

Post a Comment

Flag Counter