LOGCAT中的日志导出

import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Environment;
import android.widget.Toast;

public class LogCat {
 public static void getLogcat() {
  String shell = "logcat -d";
  try {
   Process process = Runtime.getRuntime().exec(shell);
   InputStream inputStream = process.getInputStream();

   boolean sdCardExist = Environment.getExternalStorageState().equals(
     android.os.Environment.MEDIA_MOUNTED);
   File dir = null;
   if (sdCardExist) {
    dir = new File(Environment.getExternalStorageDirectory()
      .toString() + File.separator + "logcatibb.txt");
    if (!dir.exists()) {
     dir.createNewFile();
    }else{
     dir.delete();
     dir.createNewFile();
    }

   }
   byte[] buffer = new byte[1024];
   int bytesLeft = 5 * 1024 * 1024; // Or whatever
   try {
    FileOutputStream fos = new FileOutputStream(dir);
    try {
     while (bytesLeft > 0) {
      int read = inputStream.read(buffer, 0,
        Math.min(bytesLeft, buffer.length));
      if (read == -1) {
       throw new EOFException("Unexpected end of data");
      }
      fos.write(buffer, 0, read);
      bytesLeft -= read;
     }
    } finally {
     fos.close(); // Or use Guava's
         // Closeables.closeQuietly,
     // or try-with-resources in Java 7
    }
   } finally {
    inputStream.close();
   }
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
}

你可能感兴趣的:(LOGCAT中的日志导出)