javaexcelandroid-studioapache-poihssfworkbook

Unable to write/record continuous data to a .xls sheet using ApachePOI android studio


Hi I'm currently working on my Final Year Project.. For that i need to "record continuous data of sensors from textviews to a .xls sheet". For that i'm using Apache POI library of java that allows you to create excel workbooks and sheets etc. The thing is my code is able to successfully create the sheets. But issue comes when i try to record changing values of accelerometer sensor(lets take accelerometer for the discussion only) and write those to the file, on a button click. Here's my code from .java file of android studio project.

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

public class MainActivity extends AppCompatActivity implements SensorEventListener, View.OnClickListener {

    public static int i = 1;
    public static boolean a = false;
    public static String filename;

    private SensorManager sensorManager;
    Sensor accelerometer, gyroscope, magnetometer;
    TextView ax, ay, az, gx, gy, gz, mx, my, mz;
    Button btnStart, btnStop;
    Workbook workbook;

    //ArrayList<String> array = new ArrayList<>();
    FileOutputStream fos = null;

    HSSFSheet sheet;
    Row row;
    int rownum = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ax = findViewById(R.id.valueX);
        ay = findViewById(R.id.valueY);
        az = findViewById(R.id.valueZ);

        gx = findViewById(R.id.gvalueX);
        gy = findViewById(R.id.gvalueY);
        gz = findViewById(R.id.gvalueZ);

        mx = findViewById(R.id.mvalueX);
        my = findViewById(R.id.mvalueY);
        mz = findViewById(R.id.mvalueZ);
        //---------------------------------------------------------------------------------------------------//

        btnStart = findViewById(R.id.btnStart);
        btnStop = findViewById(R.id.btnStop);

        btnStart.setOnClickListener(this);
        btnStop.setOnClickListener(this);

        filename = "File.xls";
        //This is the file, that will be made workbook when doing "workbook.write(fos{filename..})"
        workbook = new HSSFWorkbook();  //creates workbook only once when program is started

        sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        assert sensorManager != null;
        accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
        magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

        registerListeners(); //a Function.
    }

      private void registerListeners(){
        //Registering the listeners to start recording data
        if (accelerometer != null) {
            sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
        } else {
            Toast.makeText(this, "Accelerometer is not supported", Toast.LENGTH_SHORT).show();
        }

        if (gyroscope != null) {
            sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_NORMAL);
        } else {
            Toast.makeText(this, "Gyroscope is not supported", Toast.LENGTH_SHORT).show();
        }

        if (magnetometer != null) {
            sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
        } else {
            Toast.makeText(this, "Magnetometer is not supported", Toast.LENGTH_SHORT).show();
        }

    }

    //THIS IS THE PORTION WITH ISSUE .-.
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {

        Sensor sensor = sensorEvent.sensor;
        if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            ax.setText(String.valueOf(sensorEvent.values[0]));
            ay.setText(String.valueOf(sensorEvent.values[1]));
            az.setText(String.valueOf(sensorEvent.values[2]));
        } 
        else if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {

            gx.setText(String.valueOf(sensorEvent.values[0]));
            gy.setText(String.valueOf(sensorEvent.values[1]));
            gz.setText(String.valueOf(sensorEvent.values[2]));
        } 
        else if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {

            mx.setText(String.valueOf(sensorEvent.values[0]));
            my.setText(String.valueOf(sensorEvent.values[1]));
            mz.setText(String.valueOf(sensorEvent.values[2]));

        //Here i try to record sensor values... BUT it fails......!!!!!!!!!!!!!
        if(a) {

                if(row!=null) {
                    row = sheet.createRow(rownum);  //rownum =3 (initially)
                    //picking accelerometer values only.
                    row.createCell(0).setCellValue(ax.getText().toString());
                    row.createCell(1).setCellValue(ay.getText().toString());
                    row.createCell(2).setCellValue(az.getText().toString()); 
                }
                try {
                    fos = openFileOutput(filename, MODE_APPEND);
                    ((HSSFWorkbook) workbook).write(fos);
                    Toast.makeText(this, "Done" , Toast.LENGTH_SHORT).show();

                }
                catch (IOException e) {
                    e.printStackTrace();
                }
                rownum++;
            }
      }
 }
     public void onAccuracyChanged(Sensor sensor, int i) {
        //leave it empty for now
      }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnStart:
                if(createSheet()){
                    a = true;
                }
                else Toast.makeText(this, "Sheet could not be created", Toast.LENGTH_SHORT).show();
                //i++;
                break;
            case R.id.btnStop:
                a = false;
                //i++;
                Toast.makeText(this, "Values Recorded Successfully", Toast.LENGTH_LONG).show();
                /*try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } */
                break;
        }
    }

     private boolean createSheet() {  

        try{
            Toast.makeText(this, " " + i, Toast.LENGTH_SHORT).show();
            sheet = ((HSSFWorkbook) workbook).createSheet("Sheet_" + i);
            //Toast.makeText(this, "Sheet " + i, Toast.LENGTH_SHORT).show();
            Cell cell;

            row = sheet.createRow(0);
            cell = row.createCell(0);
            cell.setCellValue("Mobile Phone Sensors Readings");
            //row.createCell(0).setCellValue("Mobile Phone Sensors Readings");

            row = sheet.createRow(1);
            row.createCell(0).setCellValue("Accelerometer");
            row.createCell(3).setCellValue("Gyroscope");
            row.createCell(6).setCellValue("Magnetometer");

            row = sheet.createRow(2);
            row.createCell(0).setCellValue("X-Axis");
            row.createCell(1).setCellValue("Y-axis");
            row.createCell(2).setCellValue("Z-axis");

            row.createCell(3).setCellValue("X-Axis");
            row.createCell(4).setCellValue("Y-axis");
            row.createCell(5).setCellValue("Z-axis");

            row.createCell(6).setCellValue("X-Axis");
            row.createCell(7).setCellValue("Y-axis");
            row.createCell(8).setCellValue("Z-axis");
            try {
                fos = openFileOutput(filename, MODE_APPEND);
                ((HSSFWorkbook) workbook).write(fos);

            }
            catch (IOException e) {
                e.printStackTrace();
                return false;
            }

        }   //try1 ends.
        catch (Exception ex){
            ex.printStackTrace();
            return false;
        }  //catch for try1 ends.

        return true;
    } //createSheet function ends. 
}


Can someone help? The problem area is in onSensorValuesChanged() function.

Thanks is advance.


Solution

  • I fixed this issue for myself! Thought of mentioning it here if anyone else wants to see the answer as well. The main problem was with try-catch.. I was recording the same values to my .xls sheet.