Android – Read text file from SD-card

By -
Problem: How to read simple text file from SD-card?

First of all, let me give you a link: Environment class and getExternalStorageDirectory() method, through this method we gets the root directory of sd-card, because if we write only “/sdcard” as a static value then there may be a chance of problem because there may be having /mnt/sdcard as a root directory in some of android devices.

Now, we use this method as below:

File dir = Environment.getExternalStorageDirectory();

And the rest of the procedure I have given and described by making comments in the example so now go through the full solutions provided below with the output snap.


package com.paresh.readfilesdcard;

package com.paresh.readfilesdcard;


import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;

 * @author Paresh N. Mayani
 * @Website

public class ReadFileSDCardActivity extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {

        //Find the view by its id
        TextView tv = (TextView)findViewById(;

        File dir = Environment.getExternalStorageDirectory();
        //File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");

        //Get the text file
        File file = new File(dir,"text.txt");
        // i have kept text.txt in the sd-card

        if(file.exists())   // check if file exist
        	  //Read text from file
            StringBuilder text = new StringBuilder();

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

                while ((line = br.readLine()) != null) {
            catch (IOException e) {
                //You'll need to add proper error handling here
            //Set the text
        	tv.setText("Sorry file doesn't exist!!");

Download Full source code from here: Android – Read text file from SD-Card.

Paresh Mayani

Lazy android developer, exploring the horizon of android development since 7 years, currently working as a Technical Lead - Android at Simform Solutions, India. Former Application Architect at KarConnect. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

Loading Facebook Comments ...
Loading Disqus Comments ...