I am working on an application that records acceleration data from the smartphone and save them into a file everytime i click on the start recording button, but i can't find the created file alhough no exceptions have been raised.
I added the permission request <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in the manifest file and i checked the previously asked questions related to this topic but i still can't find where the problem is?
Here is the code :
public class MainActivity extends AppCompatActivity implements SensorEventListener {
    int clicknumber = 0;
    private static final String LOG_TAG ="";
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private Button mstartButton= null;
    private boolean servicestatus = false;
    private RelativeLayout Rl = null;
    private LinearLayout ll= null;
    long timeOffsetMs= System.currentTimeMillis()-System.nanoTime() / 1000000;
    File file=null;
    BufferedWriter out = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        final String path = Environment.getExternalStoragePublicDirectory
                (Environment.DIRECTORY_DOCUMENTS).getPath();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        servicestatus = isMyServiceRunning(RecordService.class);
        mstartButton = (Button) findViewById(R.id.recordButton);
        mstartButton.setText("Start Recording");
        mstartButton.setTextColor(Color.GREEN);
        Rl= (RelativeLayout) findViewById(R.id.Rlayout);
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        Rl.addView(ll);
        final Intent service = new Intent(this, RecordService.class);
        mstartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clicknumber+=1;
                if (clicknumber % 2 != 0 || clicknumber == 0  ){
                    startService(service);
                    mstartButton.setText("Stop Recording");
                    mstartButton.setTextColor(Color.RED);
                    String newpath = path +"data.txt";
                    file = new File(newpath);
                }
                else{
                    stopService(service);
                    mstartButton.setText("Start Recording");
                    mstartButton.setTextColor(Color.GREEN);
                }
      }
        });
    }
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);
    }
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        Sensor mySensor=sensorEvent.sensor;
        if (mySensor.getType()==Sensor.TYPE_ACCELEROMETER){
            float x=sensorEvent.values[0];
            float y=sensorEvent.values[1];
            float z=sensorEvent.values[2];
            long  timestamp=timeOffsetMs+ sensorEvent.timestamp / 1000000;
            if (isMyServiceRunning(RecordService.class) == true){
                TextView tv = new TextView(this);
                tv.setTextColor(Color.WHITE);
                tv.setText(String.format("t=%d,x=%.1f,y=%.1f,z=%.1f", timestamp,x,y,z));
                ll.addView(tv);
                try
                {
                    BufferedWriter out = new BufferedWriter(new FileWriter(file));
                    out.write(Float.toString(x) + Float.toString(y) +
                            Float.toString(z) );
                    out.close();
                }
                catch (IOException e)
                {
                    Log.e(MainActivity.LOG_TAG ,"Exception");
                }
            }
        }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {}
    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service :
                manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}
Thanks in advance!
 
    