0

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.

Burkhard
  • 14,596
  • 22
  • 87
  • 108
Ginbak
  • 105
  • 9

1 Answers1

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
  • that leads to the described problem: open failed: ENOENT (No such file or directory) when I use the "Environment.getExternalStorageState()" it says mounted and when I disconnect it to be MTP eclipse cannot find it... – Ginbak Sep 14 '14 at 13:01
  • so I changed it to:

    `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
  • sorry for the terrible formating... wouldn't say the help is to helpful :D – Ginbak Sep 14 '14 at 13:30
  • It was the correct answer, just that the phone needed to be rebooted so that that "external" storage could be created on the "internal" storage... or it was just to stupidto make it this time. A good thing is also to use the MediaScannerConnection.scanFile to let everything be updated right away for the user. But now another question: Can I only do this in the activity thread? how can I write date from another thread (I want to "stop" my data thread producing new data so nothing goes lost in the event queue if writing into file is to slow)? – Ginbak Sep 14 '14 at 22:08
  • 'how can I write date from another thread'. you need to create a AsyncTask or IntentService Class, follow : http://stackoverflow.com/questions/9671546/asynctask-android-example – S.M_Emamian Sep 15 '14 at 06:20
  • I know what you mean but it is not what I am looking for. Does stuff like "runOnUiThread" only work for async tasks or for everything? just imagine you have a normal java thread, how can you use this code up there? cause Environment is not available. – Ginbak Sep 18 '14 at 15:43