Showing posts with label create txt file and stored into in SD card. Show all posts
Showing posts with label create txt file and stored into in SD card. Show all posts

Thursday, 28 November 2013

Create txt File And Store In SDCard

Hi Friends,

Here i am going to explain how to create a txt file and stored into in SD card of mobile phone, checkout the following codes:

Example:





Codes:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Create txt file" />

</LinearLayout>


MainActivity.java

package in.androiddevelopmentanddiscussion.createtxtfileinsdcard;

import java.io.File;
import java.io.FileWriter;

import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {

Button create;
FileWriter fWriter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

create = (Button)findViewById(R.id.button1);

create.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

       try{
            fWriter = new FileWriter(Environment.getExternalStorageDirectory()+ File.separator + "File.txt");
            fWriter.write("Contents of the File");
            fWriter.flush();
            fWriter.close();
            Toast.makeText(MainActivity.this, "File Created in SD Card", Toast.LENGTH_SHORT).show();
        }catch(Exception e){
                 e.printStackTrace();
        }

}
});


}


}


Manifest

Add permissions 

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

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


Download full project from here



Check Android Apps on Google Play

https://play.google.com/store/apps/developer?id=Metro%20App%20Solution&hl=en