登录到Android上的文件文件、Android

2023-09-12 07:29:12 作者:人不疯狂枉骚年

有没有办法从Android手机中检索日志消息的。

我要建,它使用我的HTC Hero的GPS应用。我可以运行和调试从Eclipse中的应用程序,但是这不是一个很好的用例的GPS,坐在书桌前。

当我火了,当我走在应用的时候,我得到一个间歇性异常。反正我有可以输出这些异常SD卡或输出呼叫上的文本文件, Log.x()到一个文本文件,这样我可以看到什么例外情况是。

感谢

编辑:解

下面是code我最后用...

  Thread.currentThread()。setUncaughtExceptionHandler(新Thread.UncaughtExceptionHandler(){
    @覆盖
    公共无效uncaughtException(线程的线程,可抛出前){

    PrintWriter的PW;
    尝试 {
        PW =的新PrintWriter(
                新的FileWriter(Environment.getExternalStorageDirectory()+/ rt.log,真));
        ex.printStackTrace(PW);
        pw.flush();
        pw.close();
    }赶上(IOException异常E){
        e.printStackTrace();
    }
}
});
 
Android登陆界面采用文件存储实现

我只好换行

  PW =的新PrintWriter(新的FileWriter(Environment.getExternalStorageDirectory()+/ rt.log,真));
 

在一个try / catch作为Eclipse中不会让我编译应用程序。它不停地说。

 未处理的异常类型IOException异常

1速战速决
    环绕声与try / catch语句
 

所以,我没有和它所有的作品是没意见,但它确实让我不知道Eclipse的是在约...

解决方案

您可以使用Thread.setUncaughtExceptionHandler()赶上例外。

写入SD卡很简单,只要检索使用该卡Environment.getExternalStorageDirectory()并创建一个文件存在。

 文件f =新的文件(Environment.getExternalStorageDirectory(),文件名);
 

您需要给您通过添加到您的清单应用程序写入SD卡正确的权限:

 <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
 

Is there any way of retrieving log messages from an Android handset.

I'm building an application which uses the GPS of my HTC Hero. I can run and debug the application from eclipse but this isn't a good use case of GPS, sat at my desk.

When I fire the app up when I am walking around, I get an intermittent exception. Is there anyway I can output these exceptions to a text file on the SD card or output calls to Log.x("") to a text file so that I can see what the exception is.

Thanks

EDIT : Solution

Here is the code I finally went with...

Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
    @Override
    public void uncaughtException(Thread thread, Throwable ex) {

    PrintWriter pw;
    try {
        pw = new PrintWriter(
                new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true));
        ex.printStackTrace(pw);
        pw.flush();
        pw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
});

I had to wrap the line

pw = new PrintWriter(new FileWriter(Environment.getExternalStorageDirectory()+"/rt.log", true));

in a try/catch as Eclipse would not let me compile the app. It kept saying

Unhandled exception type IOException

1 quick fix
    Sorround with try/catch

So I did and it all works which is fine by me but it does make me wonder what Eclipse was on about...

解决方案

You could use Thread.setUncaughtExceptionHandler() to catch the Exceptions.

Writing to SD Card is as simple as retrieving the directory for the card using Environment.getExternalStorageDirectory() and creating a file there.

File f = new File(Environment.getExternalStorageDirectory(),filename);

You will need to give you app the correct permission to write to the SD Card by adding this to your Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />