4

我已经编写了获取 GPS 数据的代码并将其打印在 Toast 消息中。GPS 数据打印为 Toast 消息。但在应用程序关闭时不要写入 CSV 文件。这是我的代码,

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();
                }
            }
        }
    }
}
4

1 回答 1

1

我已经在 AndroidManifest 文件中声明了这一点

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

现在工作正常

于 2012-09-22T10:36:21.017 回答