• Latest Code...

    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 !!!

    No comments:

    Post a Comment


    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace