Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8569839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:25:44+00:00 2026-06-11T18:25:44+00:00

I have write code for get GPS data and print it in Toast message.

  • 0

I have write code for get GPS data and print it in Toast message. GPS Data printed as Toast message. but not write in CSV file that time the app get closed.
Here is my code,

public class MyLocationListener implements LocationListener
{
    public void onLocationChanged(Location loc)
    {
        if(root1.canWrite())
        {
            dir1 = new File (root1.getAbsolutePath() + "/TrackingData");

            if(!dir1.exists())
            {
                Toast.makeText(getBaseContext(), "Directory creation", Toast.LENGTH_LONG).show();
                dir1.mkdirs();
                file1 = new File(dir1, "Data.csv");
            }
        }

        try {
            out = new FileOutputStream (file1,append);

        } catch (FileNotFoundException e) {
            e.printStackTrace();

        }
        mlongitude = loc.getLongitude();  
        mlatitude = loc.getLatitude();

        //  location = loc;
        if (mlatitude != 0 && mlongitude !=0)
        {
            Toast.makeText( getApplicationContext(),"Lat "+mlatitude+" Long "+mlongitude,Toast.LENGTH_SHORT).show();

            //Writing in CSV file on device     

            //String SDcardpath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/TrackData/Data.csv";
            strlat = String.valueOf(mlatitude);
            strlong = String.valueOf(mlongitude );
            String strspeed = String.valueOf(speed);
            locationManager.removeUpdates(this);
            locationManager.removeUpdates( mlocListener);
            //send location to server

            if(file1.exists())
            {
                sb1.append("Time");
                sb1.append(",");
                sb1.append("Latitude");
                sb1.append(",");
                sb1.append("Longitude");
                sb1.append(",");
                sb1.append("Speed");
                if (root1.canWrite()){
                    try {
                        out.write(sb1.toString().getBytes());
                        out.write('\n');
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    sb1.delete(0, sb1.length());
                }
                sb1.append("\""+sdate+"\"");
                sb1.append(",");
                sb1.append("\""+strlat+"\"");
                sb1.append(",");
                sb1.append("\""+strlong+"\"");
                sb1.append(",");
                sb1.append("\""+strspeed+"\"");

                if (root1.canWrite()){

                    try {
                        out.write(sb1.toString().getBytes());
                        out.write('\n');
                    } 
                    catch (IOException e) 
                    {
                        Toast.makeText(getBaseContext(), "Can't write in file", Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }

                    sb1.delete(0, sb1.length());
                }
            }
            else
            {
                try {
                    file1.createNewFile();

                    sb1.append("Time");
                    sb1.append(",");
                    sb1.append("Latitude");
                    sb1.append(",");
                    sb1.append("Longitude");
                    sb1.append(",");
                    sb1.append("Speed");
                    if (root1.canWrite()){
                        try {
                            out.write(sb1.toString().getBytes());
                            out.write('\n');
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        sb1.delete(0, sb1.length());
                    }
                    sb1.append("\""+sdate+"\"");
                    sb1.append(",");
                    sb1.append("\""+strlat+"\"");
                    sb1.append(",");
                    sb1.append("\""+strlong+"\"");
                    sb1.append(",");
                    sb1.append("\""+strspeed+"\"");

                    if (root1.canWrite()){

                        try {
                            out.write(sb1.toString().getBytes());
                            out.write('\n');
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        sb1.delete(0, sb1.length());
                    }
                } catch (IOException e) {
                    Toast.makeText(getBaseContext(), "Can;t create new file", Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:25:45+00:00Added an answer on June 11, 2026 at 6:25 pm

    I have declared this in AndroidManifest file

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    Now working fine

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So far I have managed to write some code that should print the source
I have this code to write to a file, it works perfect but I
I have this problem--- Write code that will print a multiplication table for 10
I have write this code but it does work only if I am already
Sometimes I have to write code that alternates between doing things and checking for
I have been trying to write some code in Scala to read a file
My problem is that I have to write code to check if a customers
To get all groups in Active Directory i have write this code in C#.
I have to write code to clone a database entry with associated data in
I have develop a Telerik Radscheduler in that i have write the code like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.