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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了保存android程序崩溃日志到SD卡相关的知识,希望对你有一定的参考价值。


	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卡的主要内容,如果未能解决你的问题,请参考以下文章

Android 片段不保存状态,在旋转/屏幕锁定/返回时崩溃

从android寻呼机将图像保存到sd卡

Android 将位图保存到 SD 卡

Android在保存到SD卡之前拍照并调整大小

[android] 保存文件到SD卡

Android 默认相机崩溃