I am trying to write an Android program to accept input and then write the data to a file on the internal storage of my phone. When I execute my program on the emulator it makes a file just as I expect it to, but when I try and execute it on my phone the file doesn’t seem to be created. I’ve looked around on this site and I can’t seem to find a solution to my problem anywhere (a few similar ones, but nothing that worked for me) I was hoping you guys might be able to offer me advice on what I’m doing wrong.
FileOutputStream fos = null;
String x = "Sample String";
try{
fos = openFileOutput("answers.txt", MODE_PRIVATE);
fos.write(x.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
On the emulator, I can find the file in DDMS at “data/data/com.example.helloandroid/files/answers.txt”. On the phone, if I try to open up the data folder in DDMS it shows it is an empty directory. I have already added in this line to my manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I cannot seem to think of anything else that I could be doing wrong. Thanks in advance for the help, it is appreciated.
Your code is fine, but like others have said DDMS in Eclipse does not have the proper access to the internal storage, which is where /data/data lives. You can find a reference to the different types of Android storage at http://developer.android.com/guide/topics/data/data-storage.html
One way to get access is to root your device.
Another way is to install a userdebug build.
The easiest way however is via the undocumented
run-asoption of adb. This allows you to access files owned by your app, even if they are in internal storage.To verify that the file you are interested in is being created, type in at the command line:
To get the contents of a specific file, type in, also at the command line: