I am attempting to capture sensor data in a textfile that I can extract from the 'external storage' of the phone when I connect it via USB to a PC so I can run the textfile through a python script for graphing purposes. I have not implemented the sensor part yet as I am unable to create files that I can copy over to the PC from the phone after they have been created.
I attempt to create a folder and it does not create the folder and I also attempt to create a file and it also fails and then it throws an exception that the file does not exist. I am at a loss as I have added the permission to the manifest file as you can see below. Is there anything that I am missing? I have done a lot of searching around and there does not seem to be any solution to my problem and the developer.android.com website does not explain anything regarding permissions outside of adding the lines I already added to the manifest file.
I do not own an SDcard so using the sdcard is not a possible solution to my problem.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mike.project_final">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
and here is my entire code that I have done so far which is throwing the errors.
package com.example.mike.project_final;
import android.content.Context;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.view.View;
import android.widget.TextView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
TextView xTV, yTV, zTV, writeTV;
SensorManager sManager;
Sensor sAccelerometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xTV = (TextView) findViewById(R.id.xTV);
yTV = (TextView) findViewById(R.id.yTV);
zTV = (TextView) findViewById(R.id.zTV);
writeTV = (TextView) findViewById(R.id.writeTV);
}
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
public void createFile(View view) {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/project_test");
if (!dir.exists()){
dir.mkdir();
xTV.setText("Created");
}
else{
xTV.setText("Exists");
}
File file = new File(dir, "myData.txt");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("Hi , How are you");
pw.println("Hello");
pw.flush();
pw.close();
f.close();
} catch (Exception e) {
writeTV.setText(e.toString());
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy){
//do nothing
}
@Override
public void onSensorChanged(SensorEvent event) {
}
}
Any help with how to setup the rest of the permissions if required will be really appreciated!
EDIT 1: The issue with permissions have been solved, thank you. I now have an issue that the folder I create does not create as a folder but as a file as shown in the image below. What exactly am I doing wrong? https://i.stack.imgur.com/yLNsA.png Unfortunately I don't have enough reputation to post images.