. f Creating and Storing Log File on device in Android ~ Android Developers Blog

Thursday, 20 March 2014

Creating and Storing Log File on device in Android


Hello Friends,
        Today, I am sharing another android tutorial which helps you in Creating
and Storing Log File in your android phone.


1. MainActivity.java
  1. package com.example.logfileimplementation;  
  2.   
  3. import java.io.File;  
  4.   
  5. import android.os.Bundle;  
  6. import android.os.Environment;  
  7. import android.app.Activity;  
  8. import android.util.Log;  
  9. import android.view.Menu;  
  10. import android.widget.TextView;  
  11.   
  12. public class MainActivity extends Activity {  
  13.    
  14.  public static final String SDCARD = String.valueOf(Environment  
  15.    .getExternalStorageDirectory());  
  16.  TextView mTextView;  
  17.  @Override  
  18.  protected void onCreate(Bundle savedInstanceState) {  
  19.   super.onCreate(savedInstanceState);  
  20.   try {  
  21.   deleteLog();  
  22.   setContentView(R.layout.activity_main);  
  23.   createAppDirectories();  
  24.   mTextView = (TextView) findViewById(R.id.action_settings);  
  25.   mTextView.setText("Android crash Reporter");  
  26.   } catch(Exception ex) {  
  27.    Utilities.writeIntoLog(Log.getStackTraceString(ex));  
  28.   }  
  29.  }  
  30.   
  31.  @Override  
  32.  public boolean onCreateOptionsMenu(Menu menu) {  
  33.   // Inflate the menu; this adds items to the action bar if it is present.  
  34.   getMenuInflater().inflate(R.menu.main, menu);  
  35.   return true;  
  36.  }  
  37.   
  38.  // Creating App Directory For Log File  
  39.  private void createAppDirectories() {  
  40.   System.out.println("hiii");  
  41.   File dir1 = new File(Utilities.APP_DIR);  
  42.   if (!dir1.exists()) {  
  43.    dir1.mkdir();  
  44.   }  
  45.  }  
  46.   
  47.  // Deleting App Directory which is used for Log File  
  48.  private void deleteLog() {  
  49.   
  50.   File log = new File(Utilities.LOG_FILE_PATH);  
  51.   if (log.exists()) {  
  52.    log.delete();  
  53.   }  
  54.  }  
  55. }  
2. Utilities.java
  1. package com.example.logfileimplementation;  
  2.   
  3. import java.io.BufferedWriter;  
  4. import java.io.FileWriter;  
  5.   
  6.   
  7. public class Utilities {  
  8.  public static String APP_DIR = MainActivity.SDCARD+"/Logfile";  
  9.  public static String LOG_FILE_PATH = APP_DIR+"/ myapp_log.txt";    
  10.    
  11.    
  12.    
  13.  public static void writeIntoLog(String data)   
  14.  {  
  15.      
  16.   FileWriter fw = null;      
  17.   try {  
  18.      
  19.    fw = new FileWriter(LOG_FILE_PATH , true);  
  20.    BufferedWriter buffer = new BufferedWriter(fw);     
  21.    buffer.append(data+"\n");  
  22.       
  23.    buffer.close();  
  24.   
  25.   } catch (Exception e) {   
  26.    e.printStackTrace();  
  27.   }  
  28.      
  29.  }    
  30. }  
3. activity_main.xml


  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.   
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.   
  5.     android:layout_width="match_parent"  
  6.   
  7.     android:layout_height="match_parent"  
  8.   
  9.     android:paddingBottom="@dimen/activity_vertical_margin"  
  10.   
  11.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  12.   
  13.     android:paddingRight="@dimen/activity_horizontal_margin"  
  14.   
  15.     android:paddingTop="@dimen/activity_vertical_margin"  
  16.   
  17.     tools:context=".MainActivity" >  
  18.   
  19.   
  20.   
  21.     <TextView  
  22.   
  23.         android:id="@+id/textView"  
  24.   
  25.         android:layout_width="wrap_content"  
  26.   
  27.         android:layout_height="wrap_content"  
  28.   
  29.         android:text="@string/hello_world" />  
  30.   
  31.     
  32.   
  33. </RelativeLayout>  
4. AndroidManifest.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.logfileimplementation"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="18" />  
  10.   
  11.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>  
  12.   
  13.     <application  
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name="com.example.logfileimplementation.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  
Hope this will help you.
Enjoy Coding. Cheers.... :)

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.