Saving Logcat to a text file in Android Device

前端 未结 7 1746
时光取名叫无心
时光取名叫无心 2020-12-02 08:16

I had found some crashes while running the application in android device, which is not showing in emulator. So i need to save the Logcat in a text file in my device\'s memor

7条回答
  •  遥遥无期
    2020-12-02 08:37

    Use an Application class at the beginning of your app. That allows a proper file and log handling.

    Code below creates a log file at the following location:

    /ExternalStorage/MyPersonalAppFolder/logs/logcat_XXX.txt
    

    XXX is the current time in milliseconds. Every time you run your app, a new logcat_XXX.txt file will be created.

    public class MyPersonalApp extends Application {
    
        /**
         * Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
         */
        public void onCreate() {
            super.onCreate();
    
            if ( isExternalStorageWritable() ) {
    
                File appDirectory = new File( Environment.getExternalStorageDirectory() + "/MyPersonalAppFolder" );
                File logDirectory = new File( appDirectory + "/logs" );
                File logFile = new File( logDirectory, "logcat_" + System.currentTimeMillis() + ".txt" );
    
                // create app folder
                if ( !appDirectory.exists() ) {
                    appDirectory.mkdir();
                }
    
                // create log folder
                if ( !logDirectory.exists() ) {
                    logDirectory.mkdir();
                }
    
                // clear the previous logcat and then write the new one to the file
                try {
                    Process process = Runtime.getRuntime().exec("logcat -c");
                    process = Runtime.getRuntime().exec("logcat -f " + logFile);
                } catch ( IOException e ) {
                    e.printStackTrace();
                }
    
            } else if ( isExternalStorageReadable() ) {
                // only readable
            } else {
                // not accessible
            }
        }
    
        /* Checks if external storage is available for read and write */
        public boolean isExternalStorageWritable() {
            String state = Environment.getExternalStorageState();
            if ( Environment.MEDIA_MOUNTED.equals( state ) ) {
                return true;
            }
            return false;
        }
    
        /* Checks if external storage is available to at least read */
        public boolean isExternalStorageReadable() {
            String state = Environment.getExternalStorageState();
            if ( Environment.MEDIA_MOUNTED.equals( state ) ||
                    Environment.MEDIA_MOUNTED_READ_ONLY.equals( state ) ) {
                return true;
            }
            return false;
        }
    }
    

    you need the correct permissions and name of your application class in your .manifest file:

    
    
    
    
    
    

    Edit:

    if you want to save log of only some particular activities..

    replace:

    process = Runtime.getRuntime().exec("logcat -f " + logFile);
    

    with:

    process = Runtime.getRuntime().exec( "logcat -f " + logFile + " *:S MyActivity:D MyActivity2:D");
    

提交回复
热议问题