任何人都可以告诉如何在文本文件中编写异常并将其保存在android
中的sdcard中由于
答案 0 :(得分:5)
试试这个
try{
..........
}catch(Exception e){
PrintWriter pw = new PrintWriter(new FileOutputStream("Log"));
e.printStackTrace(pw);
}
或查看此博客
http://www.digitalprank.org/how-to-redirect-console-output-to-file-using-java/
答案 1 :(得分:3)
我认为您正在尝试在应用程序中实现崩溃/错误日志记录系统。 this is a superb way to do it
或者您始终可以使用e.printStackTrace()并将其转换为字符串并将其写入SD卡上的文本文件。查看here
代码而不是使用电子邮件意图只需打开SD卡中的.txt文件并将字符串写入其中。
答案 2 :(得分:0)
您也可以创建一个Logger-Class。
def measure(&block)
start = Time.now
block.call
Time.now - start
end
# t1 and t2 is the executing time for the code blocks.
t1 = measure { sleep(1) }
t2 = measure do
sleep(2)
end
只需更改SD卡的路径即可。
在OnCreate-Method
中创建一个新实例 /// <summary>
/// Init the logger
/// </summary>
public LogUtils()
{
String path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/MyAppFolder";
logFilePath = System.IO.Path.Combine(path, "LogFile.txt");
if (!System.IO.File.Exists(logFilePath))
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(logFilePath, true))
{
writer.WriteLine("Starting logging at " + DateTime.Now.ToString() + "\n");
}
}
}
/// <summary>
/// Logs new messages
/// </summary>
/// <param name="message"></param>
public void Log(String message)
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(logFilePath, true))
{
writer.WriteLine(DateTime.Now.ToString() + " : " + message + "\n");
}
}
并记录消息
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
LogUtils Logger = new LogUtils();