• 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;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.readfile);
           tvRead=(TextView)findViewById(R.id.tv_read);
    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(line);
          //text.append('\n'); // commented for one line only
       }
       br.close();
    }
    catch (IOException e) {
       e.printStackTrace();
    }

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

    Circle

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