-1

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!

Houssem
  • 3
  • 1
  • https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl – CommonsWare May 20 '17 at 23:04

1 Answers1

0

Your File is at: /storage/emulated/0/Documentsdata.txt

Note that 2 things should meet, because the error here is not intended.

  1. The Directory Document must exist.
  2. You forgot / in the following line
    Error : String newpath = path +"data.txt" No Error : String newpath = path +"/"+"data.txt"

If you test your code now, you can get you file or an exception is thrown.

which api are you using ?. declaring the permission in the manifest for writing file to a Storage is neccessary. but for the completness you should have granted permission from the user if the system api > 23.

momo
  • 16
  • 2
  • Thanks for your help, I corrected the mistake ("/") but the problem still there. I also made sure that the directory Document exists. – Houssem May 21 '17 at 11:52
  • The problem is solved thanks, the system api was 25 so I granted storage permission for the application from `Settings > apps`..! – Houssem May 21 '17 at 22:16