1

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

csciBeginner
  • 219
  • 1
  • 4
  • 15

1 Answers1

3

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*/
Community
  • 1
  • 1
Shrijit Basak
  • 307
  • 1
  • 13
  • @csciBeginner you can check this as well http://stackoverflow.com/questions/13006315/how-to-access-data-data-folder-in-android-device – Todor Kostov Jul 27 '16 at 21:05
  • I thought you can't access a database. I thought a csv or txt would be fine because it would be placed in the files folder. And on Android's sight they recommend storing data the way I used – csciBeginner Jul 27 '16 at 21:06
  • If you are testing your app on a device running Android M then you will have to ask for the permission during runtime and you may also have to give a path to store the file. You will need to use Environment.getExternalStorageDirectory() to get a path and save files there – Shrijit Basak Jul 27 '16 at 21:17
  • @shrijit can you show me how to implement that? Where would I add that into my existing code? – csciBeginner Jul 28 '16 at 14:24
  • @csciBeginner i added the code. Comment if it did not work out. – Shrijit Basak Jul 28 '16 at 18:33
  • I ended up figuring it out. I put the code I used in the edit of my post. Thanks. – csciBeginner Jul 29 '16 at 18:36