android read file from sdcard

android read file from sdcard

GTU Android Program 19 Create an application to read file from the sdcard and display that file content to the screen

GTU Android Program 19

Android Program on how to read data from sd card or an external device? Many times android programmer need to read data from sd card, here is the example to read data from the sd card which can help you to read data from sd card. In this example, I have read a text file and displayed on TextView.

You have reached to the easiest android tutorial from the beginning, Don’t forget to check out android tips and tricks and android step by the stpe guide if you are beginners. My All android tutorial will save your lot of time and to the points. Now let’s discuss the reading file from sd card in android. Here is the step by step code with output, Let’s Start!

Don’t forget to copy a readme.txt file into sd card.

Location : res\layout

FileName: activity_main.xml

 

<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:background=”#fce4ec”
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=”gtu.android.prog19.MainActivity” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textColor=”#e91e63″
android:text=”Read file from SDCard”
android:textAppearance=”?android:attr/textAppearanceLarge” />

<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:layout_marginBottom=”52dp”
android:textColor=”#9e9e9e”
android:text=”Developed By: Adarsh Patel”
android:textAppearance=”?android:attr/textAppearanceLarge” />

<TextView
android:id=”@+id/textView3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_centerHorizontal=”true”
android:layout_centerVertical=”true”
android:text=””
android:textColor=”#d81b60″
android:textAppearance=”?android:attr/textAppearanceLarge” />

</RelativeLayout>

 

 

Location : src\package\

FileName : MainActivity.java

package gtu.android_prog19;

/*
* GTU Android Program 19 Create an application to read file from the sdcard and display that file content to the screen
*
*/

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

TextView tv3;

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

//===========================================
// File path we want to read (current path : sdcard/readme.txt)
//==========================================
File file = new File( Environment.getExternalStorageDirectory() + “/readme.txt”);

//===========================================
// string builder object to hold intermediate data.
//==========================================
StringBuilder sb = new StringBuilder();

try
{
BufferedReader br = new BufferedReader(new FileReader(file));
String line;

//===========================================
// read line by line data.
//==========================================
while ((line = br.readLine()) != null)
{
sb.append(line);
sb.append(‘\n’);
}

br.close();
}
catch (IOException e)
{
Toast.makeText(getApplicationContext(), “Error While Reading File”, Toast.LENGTH_LONG).show();
}

//===========================================
// Display read text into textview3
//==========================================
tv3 = (TextView)findViewById(R.id.textView3);
tv3.setText(sb);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

 

 

Output

GTU Android Program 19

 

Have you checked ? Android Tips and Tricks

 

Few more Article on Android Learning :

Adarsh Patel

My Self Adarsh Suryakant Patel. Currently I am working as Assistant Professor at Parul Instiute of Engineering and Technology, MCA Department, Limda, Vadodara. Major Responsibility : * Lectures * Organizing Training and Expert Sessions for * Placement and Recruitment * Faculty Representative