Read File from SDCard

Frequently used code samples and tutorials
vivekkalady
Site Admin
Posts: 35
Joined: Tue Nov 18, 2014 2:39 pm

Read File from SDCard

Postby vivekkalady » Fri Nov 21, 2014 5:36 pm

This is a sample code to read a file and convert to string

Code: Select all

//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
StringBuilder text = new StringBuilder();

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

    while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
    br.close();
}
catch (IOException e) {
    //You'll need to add proper error handling here
}

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

//Set the text
tv.setText(text);


Use thread for reading the file.

so you have to use Handler for posting the string to TextView

Return to “Tutorials & Examples”

Who is online

Users browsing this forum: No registered users and 1 guest

cron