logcat

  1. String shell = "logcat";  
  2.                 try  
  3.                 {  
  4.                     Process process = Runtime.getRuntime().exec(shell);  
  5.                     InputStream inputStream = process.getInputStream();  
  6.                       
  7.                       
  8.                     boolean sdCardExist = Environment.getExternalStorageState().equals(  
  9.                             android.os.Environment.MEDIA_MOUNTED);  
  10.                     File dir = null;  
  11.                     if (sdCardExist)  
  12.                     {  
  13.                         dir = new File(Environment.getExternalStorageDirectory().toString()  
  14.                                 + File.separator + "logcatwyx.txt");  
  15.                         if (!dir.exists())  
  16.                         {  
  17.                             dir.createNewFile();  
  18.                         }  
  19.   
  20.                     }  
  21.                     byte[] buffer = new byte[1024];  
  22.                     int bytesLeft = 5 * 1024 * 1024// Or whatever  
  23.                     try  
  24.                     {  
  25.                         FileOutputStream fos = new FileOutputStream(dir);  
  26.                         try  
  27.                         {  
  28.                             while (bytesLeft > 0)  
  29.                             {  
  30.                                 int read = inputStream.read(buffer, 0, Math.min(bytesLeft,  
  31.                                         buffer.length));  
  32.                                 if (read == -1)  
  33.                                 {  
  34.                                     throw new EOFException("Unexpected end of data");  
  35.                                 }  
  36.                                 fos.write(buffer, 0, read);  
  37.                                 bytesLeft -= read;  
  38.                             }  
  39.                         } finally  
  40.                         {  
  41.                             fos.close(); // Or use Guava's  
  42.                                          // Closeables.closeQuietly,  
  43.                             // or try-with-resources in Java 7  
  44.                         }  
  45.                     } finally  
  46.                     {  
  47.                         inputStream.close();  
  48.                     }  
  49. //                    String logcat = convertStreamToString(inputStream);  
  50. //                    outputFile2SdTest(logcat, "logwyx.txt");  
  51.                     Log.v(TAG, "LOGCAT = ok" );  
  52.                 } catch (IOException e)  
  53.                 {  
  54.                     e.printStackTrace();  
  55.                 }  
posted @ 2013-06-11 10:05  火腿骑士  阅读(259)  评论(0编辑  收藏  举报