I am writing values to my database and stop the time it needs for an evaluation. Now I would like to write these times to an easy accessible file like a .txt but I can not write on the phone (some answers on the internet say because it is connected as "media source" but when I disconnect it I can not connect to eclipse anymore). So the question is: How can I write a file which I can simply copy from my phone to my PC to get the data to analyze them.
Asked
Active
Viewed 53 times
1 Answers
1
you can use below function, text
is your content:
public void backUp(String text,String namefile)
{
File file = new File(Environment.getExternalStorageDirectory()+"/yourdir/"+namefile+".txt");
if (!file.exists())
{
try
{
file.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(file, true));
buf.append(text);
buf.newLine();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
updated :
you should create your directory before use above function :
File yourdir = new File(Environment.getExternalStorageDirectory()+"/yourdir");
if(!yourdir.exists()){
yourdir.mkdir();
}
then :
backUp("hello world", "test");
don't forget this permission :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

S.M_Emamian
- 17,005
- 37
- 135
- 254
`File yourdir = new File(Environment.getExternalStorageDirectory()+"/yourdir");
if(!yourdir.exists()){
yourdir.mkdir();
}
File file = new File(Environment.getExternalStorageDirectory()+"/yourdir/"+"test"+".txt");`
and the error vanished.
But when I try to use
Environment.getExternalStorage().getAbsolutePath() it returns /`storage/emulated/0 as path. I feel a little stupid but what the heck? – Ginbak Sep 14 '14 at 13:22