Wednesday, 15 June 2016

How to write App LogCat to SD card in Android

The Android logging system provides a mechanism for collecting and viewing system debug output. Logs from various applications and portions of the system are collected in a series of circular buffers.


Img src

We can test app on Emulator and with the help of Logcat we can easily trace the Error details.
But if we are testing on Real Android Device and that is not connected with system then its hard to know where we are getting exception. So this tutorial aims to resolve this problem.

We all know how to view LogCat message in Eclipse but how to write the LogCat details into a file and save it in SD card?

Using Shell command :


To save LogCat to a text file open up a terminal window and type:
    adb logcat -d > logcat.txt

The above command will create a file named "logcat.txt" in your current directory.  The -d option indicates that you are dumping the current contents and then exiting.

Read the LogCat programmatically :

Permission in Manifest :

  1. <uses-permission android:name = "android.permission.READ_LOGS" />
  2. <uses-permission android:name = "android.permission.READ_EXTERNAL_STORAGE" />
  3. <uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" />



Step 1 : Create a new project by going to File ⇒ New Android Application Project. Fill all the details and name your activity as MainActivity. (eclipse)

Step 2 : Design the activity_main.xml layout for MainActivity under layout folder as shown below

 activity_main.xml
 
    <RelativeLayout
        android:layout_width = "fill_parent"
        android:layout_height = "fill_parent" >

        <Button
            android:id = "@+id/btnWriteLogCat"                  
            android:layout_width = "wrap_content"
            android:layout_height = "wrap_content"
            android:layout_centerHorizontal= "true"
            android:layout_centerVertical= "true"
            android:text = "Write LogCat" />

    </RelativeLayout>

Step 3 : Add the following code in onCreate() method of MainActivity.java under src folder.
MainActivity.java

findViewById(R.id.btnWriteLogCat).setOnClickListener(new OnClickListener()

{

      @Override
      public void onClick(View v)
     {
writeLogCat();
     }
});

protected void writeLogCat()
{
try
{
     Process process = Runtime.getRuntime().exec("logcat -d");
               BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream)));
              StringBuilder log = new StringBuilder();
              String line;
              while((line = bufferedReader.readLine()) != null)
              {
                     log.append(line);
                     log.append("\n");
              }

             //Convert log to string
             final String logString = new String(log.toString());
            
             //Create txt file in SD Card
             File sdCard = Environment.getExternalStorageDirectory();
             File dir = new File(sdCard.getAbsolutePath() +File.separator + "Log File");

             if(!dir.exists())
             {
                  dir.mkdirs();
             }

             File file = new File(dir, "logcat.txt");

             //To write logcat in text file
             FileOutputStream fout = new FileOutputStream(file);
             OutputStreamWriter osw = new OutputStreamWriter(fout);

             //Writing the string to file
             osw.write(logString);
             osw.flush();
             osw.close();
          }
          catch(FileNotFoundException e)
          {
               e.printStackTrace();
          }
          catch(IOException e)
         {  
               e.printStackTrace();
         }
}

You can check the generated file named "logcat.txt" inside Log File Folder under Sd card.



Path : sdcard/Log File/logcat.txt

Source link : GreenMan

As always, Thanks a lot for reading...
Don't forget to share this post if you found it interesting!
If you find something to add to this post? or any other quick thoughts/hints that you think people will find useful? Share it in the comments & feedback's are most welcome.