如何使用Android的编写异常日志文件?如何使用、异常、文件、日志

2023-09-06 07:22:23 作者:天幕

我想要写文本文件的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.