1

I am very new to Java & Android but I need to write a txt file which contains sensor data in Environment.DIRECTORY_DOCUMENTS. I scanned questions about it and tried examples. Version used here is Android 5.1 (API 22). My final trial is here:

package com.example.accgy;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    private static final String TAG = "MainActivity";
    private SensorManager sensorManager;
    Sensor accelerometer;
    Sensor gyroscope;
    FileWriter writer;
    TextView Xa,Ya,Za,Xg,Yg,Zg;
    float[] accel = new float[3];
    float[] gyro = new float[3];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Xa = (TextView)findViewById(R.id.Xa);
        Ya = (TextView)findViewById(R.id.Ya);
        Za = (TextView)findViewById(R.id.Za);
        Xg = (TextView)findViewById(R.id.Xg);
        Yg = (TextView)findViewById(R.id.Yg);
        Zg = (TextView)findViewById(R.id.Zg);
        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }


    public void onStart(View view){
        super.onStart();
        sensorManager.registerListener(this,accelerometer,SensorManager.SENSOR_DELAY_NORMAL);
        sensorManager.registerListener(this,gyroscope,SensorManager.SENSOR_DELAY_NORMAL);
        String savedirect = getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS).toString();




        try {
            File folder = new File(savedirect,"/example.txt");
            folder.mkdir();
            writer = new FileWriter(folder);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void onStop(View view) {
        super.onStop();

        sensorManager.unregisterListener(this);
        try {
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {

        if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER){
            accel = sensorEvent.values;
            Xa.setText("Xa: " + accel[0]);
            Ya.setText("Ya: " + accel[1]);
            Za.setText("Za: " + accel[2]);

        }
        if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            gyro = sensorEvent.values;
            Xg.setText("Xg: " + gyro[0]);
            Yg.setText("Yg: " + gyro[1]);
            Zg.setText("Zg: " + gyro[2]);
        }
        try {
            writer.write(accel[0]+"  "+accel[1]+"  "+accel[2]+"  "+gyro[0]+"  "+gyro[1]+"  "+gyro[2]+"\n");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  

But when I run the code over a physical device and touch the start button, the app crashes. Is there a way to prevent this?

Note: I also provide permission for writing external storages in manifest file.

EDIT: I checked logcat and found:

Java.lang.NullPointerException: Attempt to invoke virtual method 'void java.io.FileWriter.write(java.lang.String)' on a null object reference

at:

 try {
        writer.write(accel[0]+"  "+accel[1]+"  "+accel[2]+"  "+gyro[0]+"  "+gyro[1]+"  "+gyro[2]+"\n");
    } catch (IOException e) {
        e.printStackTrace();
    }

When I change this line:

writer = new FileWriter(folder);

to this:

writer = new FileWriter(folder+"example.txt",true);

App succesfully writes the data. But the name of file becomes "Documentsexample.txt". But my desire is "example.txt". I tried to remove "folder+" part from the line, tried to create folder in different ways. But same error returns after re-run. It seems no way other than above.

  • 1
    Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody Oct 17 '20 at 14:55
  • Have you tried debugging and looking at the exception? – derpirscher Oct 17 '20 at 15:32
  • And since android 6 you need to explicitly ask the user for permission. Just adding it to the manifest is not sufficient any more https://stackoverflow.com/q/33162152/3776927 – derpirscher Oct 17 '20 at 15:35
  • `File folder`. No that is not a folder. That indicates your filr. Do do not use mkdir on it as it will become a folder. – blackapps Oct 17 '20 at 19:03

0 Answers0