Read text file from SD card in android programming

Are you planning to read text file from sdcard , and get value in you code. here is the simple example to read text file from sdcard.

Here i defined gps time in text file, getting this time period and using in my application:

public class Test extends Activity {
public static String PACKAGE_NAME;
private TextView tvRead;
protected void onCreate(Bundle savedInstanceState) {
tvRead.setText("Value from SD Card="+Long.toString(getGpsTimeInterval()));

private long getGpsTimeInterval() {
        File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"PRANDROID/GPS Settings/time.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('\n'); // commented for one line only
catch (IOException e) {

String timeText=text.toString();
String [] splitTimes=timeText.split("\\=");
long returnTime=Long.parseLong(splitTimes[1]);
Log.i("returnTime", "returnTime="+returnTime);
   return returnTime;

Here file must be PRANDROID/GPS Settings folder named time.txt.

Output snapshots:

Happy Coding !!!
Read text file from SD card in android programming Read text file from SD card in android programming Reviewed by D Kshetri on 1:41 AM Rating: 5

No comments:

Powered by Blogger.