1

I have an app that write some logs like

Log.d("Device Name", android.os.Build.MODEL);

How can I write them into the .txt - file on SD card?

If i trying to run simple app, its not save any files

    package com.mycompany.txt;
import android.app.*;
import android.os.*;
import java.io.*;


public class MainActivity extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void appendLog(String text)
{       
   File logFile = new File("/mnt/sdcard/log.file");
   if (!logFile.exists())
   {
      try
      {
         logFile.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(logFile, true)); 
      buf.append(text);
      buf.newLine();
      buf.close();
   }
   catch (IOException e)
   {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
}

}

Should I get full logs in the txt file with it?

Patrick
  • 17,669
  • 6
  • 70
  • 85
user1835337
  • 656
  • 1
  • 9
  • 32
  • possible duplicate of [Android Writing Logs to text File](http://stackoverflow.com/questions/1756296/android-writing-logs-to-text-file) – Chris Stratton Jun 15 '13 at 22:00
  • Note you will typically want to make your log tag more consistent to identify your application or component, and put descriptions of what you are logging in the second parameter, ie, `Log.d("MyApp", "Device Name " + android.os.Build.Model);` – Chris Stratton Jun 15 '13 at 22:02
  • 1
    "Should I get full logs in the txt file with it?" -- no, because you are never calling `appendLog()`. – CommonsWare Jun 15 '13 at 22:34
  • How can I call it here? – user1835337 Jun 16 '13 at 07:17

1 Answers1

0

Copying from here(possible duplicate):

  public void appendLog(String text)
{       
   File logFile = new File(Environment.getExternalStorageDirectory(),"my_app_dir/my_log_file.log");
   if (!logFile.exists())
   {
      try
      {
         logFile.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(logFile, true)); 
      buf.append(text);
      buf.newLine();
      buf.close();
   }
   catch (IOException e)
   {
      // TODO Auto-generated catch block
      e.printStackTrace();
   }
}
Community
  • 1
  • 1
py_script
  • 808
  • 2
  • 16
  • 38
  • Right, it is the same on the other post too. I will fix it – py_script Jun 15 '13 at 21:57
  • That too is not necessarily a valid path. Never hardcode paths. Please use `getExternalFilesDir()` or methods on `Environment` to build valid paths to external storage. – CommonsWare Jun 15 '13 at 21:58
  • 1
    It's a bit better. Use `new File(Environment.getExternalStorageDirectory(),"my_log_file.log")` so you don't have to worry about whether `getExternalStorageDirectory()` has a trailing slash or not. Also, what you are doing is dumping the log into the root of external storage. `getExternalFilesDir()`, or maybe `Environment.getExternalStoragePublicDirectory()`, don't clutter up the user's external storage root so much. – CommonsWare Jun 15 '13 at 22:33