保存android程序崩潰日志到SD卡

jopen 10年前發布 | 15K 次閱讀 Android開發 移動開發 Android

private boolean writeToSDCard(Throwable ex)   
{  
    boolean isDealing = false;  
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))  
    {  
        RandomAccessFile randomAccessFile = null;  
        try  
        {  
            String fileName = SDCARDROOT + File.separator + "logs" + File.separator + "crash" + File.separator;  
            File file = new File(fileName);  
            if(!file.exists())  
                file.mkdirs();  
            randomAccessFile = new RandomAccessFile(fileName + paserTime(System.currentTimeMillis())+ ".log", "rw");  
            long fileLength = randomAccessFile.length();  
            randomAccessFile.seek(fileLength);  
            randomAccessFile.writeBytes(getThrowableInfo(ex));  
        }   
        catch (IOException e)   
        {  
            e.printStackTrace();  
        }   
        finally   
        {  
            if (randomAccessFile != null)  
            {  
                try   
                {  
                    randomAccessFile.close();  
                    isDealing = true;  
                }   
                catch (IOException e)  
                {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }  
    return isDealing;  
} 

private static String getThrowableInfo(Throwable ex)  
{  
    StringWriter stringWriter = new StringWriter();  
    PrintWriter printWriter = new PrintWriter(stringWriter);  
    ex.printStackTrace(printWriter);  
    return stringWriter.toString();  
}  
 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!