码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-06-26 11:58:42      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:android   class   blog   code   java   ext   


	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();
	}


保存android程序崩溃日志到SD卡,布布扣,bubuko.com

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

标签:android   class   blog   code   java   ext   

原文地址:http://blog.csdn.net/wangyuexing_blog/article/details/34799671

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!