csciBeginner csciBeginner - 4 months ago 9
Java Question

Accessing Data Saved to Internal Storage on Android Device

I am new to Android Studio and have gone through a ton of tutorials. I have a working accelerometer and want to save the data to a .csv file.
My code below works because when I use the emulator I am able to export the .csv file , with the correct data, to my desktop and open it. I have these two permissions in my

AndroidManifest.xml


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


When I go into the file explorer on my device using apps such as Astro or ES and go to
data/data/
I do not even see my package there even though the app works on my phone. Is there something you need to do to have the package show up in the file explorer? I would like to get the data from the .csv from my phone.

I was hoping someone had some in

String Entry = x.getText().toString() + ", "
+ y.getText().toString() + ", " +
z.getText().toString() + "\n";
try{
FileOutputStream outPut = openFileOutput(FILENAME, Context.MODE_APPEND);
outPut.write(Entry.getBytes());
outPut.close();
}catch(Exception err){
err.printStackTrace();
}


EDIT

String dir = Environment.getExternalStorageDirectory().getAbsolutePath();
String FILENAME = "keyboardTest.csv";
File file = new File (dir, FILENAME)
try {
FileOutputStream out = new FileOutputStream(file, true);
out.write(Entry.getBytes());
out.close();
} catch (Exception err) {
err.printStackTrace();
}


Now I am able to access the file on my phone

Answer

You can't access these files(inside /data/data) until you are running the app on an emulator or you are running the app on a rooted device.

please check this for more details.

EDIT

You can do the following to save files in an accessible location:-

/*get the path to internal storage*/
File path = Environment.getExternalStorageDirectory();
/*create the app folder and check if it exists or not*/
File curDir = new File(path.getAbsolutePath()+"/"+appName);
if(!curDir.exists()){
        curDir.mkdirs();
}
/*create the file*/
File f = new File(curDir+"/"+fileName);
f.createNewFile();
/*code to edit the file*/
Comments