如何使用Android的编写异常日志文件? [英] how to write exception in log file using android?

查看:139
本文介绍了如何使用Android的编写异常日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要写文本文件的SD卡时,我得到一个  从我的应用程序异常

I want to write text file in sd card when I get an exception from my application

如何实现这个概念我  应用?

How to implement this concept in my application?

推荐答案

去了解这一点,但实际上,你应该尽可能使用缓冲流的通用方式。

A general way to go about this, although in practice you should use buffered streams whenever possible.

在你的的onCreate(包savedInstanceState)回调或其他任何地方,你想这样做的日志:

In your onCreate(Bundle savedInstanceState) callback or anywhere else you want to do this logging:

try
{
//normal application code

}
//called whenever an Exception is thrown
catch(Exception e)
{
//stream for writing text
FileWriter writer=null;
try
{
    writer = new FileWriter(this.getExternalFilesDir());
    //write to the SD card
} catch(Throwable t) {}
finally
{
    if(writer != null) writer.close();
}

}

请注意,我省略检查SD卡,但你应该总是这样做。见的http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

Note that I omitted checking for the SD card, but you should always do this. See http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

抱歉蹩脚code格式。这个小文本框是不一样的Eclipse。

Sorry for the crappy code formatting. This little text box is nothing like Eclipse.

这篇关于如何使用Android的编写异常日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆