Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 保存android程序崩潰日志到SD卡

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

編輯:關於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();
	}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved