I try to write something into my phone memory.
At first, I hard-coded the path as:
myFile = new File("/sdcard/" + txtName.getText() + ".txt");
This works totally ok.
And then, eclipse gives me a warning saying that I shouldn't have hard-coded the path like that instead, I should do the following:
myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/"+ txtName.getText() + ".txt");
Then I follow the correction suggestion and avoid the warning.
However, I encountered a runtime exception on the writer class.
Then, I print out Environment.getExternalStorageDirectory().getAbsolutePath()
for debugging purpose. The result is
/storage/sdcard0
. But the hardcoded one that worked perfectly fine before is actually
/sdcard
.
Why would this happen?
And if I wish to avoid the warning, how can I get the path directory in a more "formal and right" way instead of hardcoding the path?
P.S.: My phone is HTC One X, which has NO external SD card slot. I guess the 32GB storage comes with a built-in SD card, and therefore, the essence should be the same.