postheadericon read file from sdcard in android example



Definition : 19) Create an application to read file from the sdcard and display that file content to the screen.


File Name : E19Activity.java


package bsr.exa;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;



import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

public class E19Activity extends Activity {
  /**
  * @author Bipin S Rupadiya , www.gtu-android.blogspot.com
  *
  * 19) Create an application to read file from the sdcard and display that file content to the screen.
  *
  *  Note: first create a file bsr.txt on sdcard, use DDMS to push file on sdcard
    */

public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.main);
try
{
File fileDir = Environment.getExternalStorageDirectory();
        File directory = new File(fileDir.getAbsolutePath());
        File file = new File(directory , "bsr.txt");
        FileInputStream fis = new FileInputStream(file);
String str = null;
StringBuffer sbuffer = new StringBuffer();
DataInputStream dataio = new DataInputStream(fis);
while((str = dataio.readLine()) != null)
{
sbuffer.append(str + "\n");
}
TextView txt=(TextView)findViewById(R.id.showTxt);
txt.setText(sbuffer);
}
catch (Exception e)
{
Toast.makeText(this, " "+e.toString(), Toast.LENGTH_LONG).show();
}
}
}


File Name : AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="bsr.exa"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".E19Activity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>




0 comments:

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.