首页 > 移动平台 > 详细

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

时间:2016-02-18 22:45:10      阅读:193      评论:0      收藏:0      [点我收藏+]


	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卡

原文:http://www.cnblogs.com/lcchuguo/p/5199349.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!