首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将日志输出从logcat重定向到android设备上的SD-Card?

如何将日志输出从logcat重定向到android设备上的SD-Card?
EN

Stack Overflow用户
提问于 2010-07-29 13:28:56
回答 6查看 26.8K关注 0票数 19

我正在尝试将我的应用程序的日志重定向到sdcard文件。但我没有做到这一点。我正在尝试这样的东西。

String cmd= "logcat -v time   ActivityManager:W  myapp:D  *:* >\""+file.getAbsolutePath()+"\"";
Runtime.getRuntime().exec(cmd);

我也尝试了-f选项,但它也不起作用。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2010-07-29 14:07:10

使用logcat -f <filename>将其转储到文件系统中的一个文件中。

确保您使用的文件名在SD卡中,即以/sdcard/...开头。

此外,为了将参数传递给logcat程序,您应该向exec方法传递一个字符串数组(而不是一个字符串):

String[] cmd = new String[] { "logcat", "-f", "/sdcard/myfilename", "-v", "time", "ActivityManager:W", "myapp:D" };

最后,如果所有其他方法都失败了,请使用logcat:/system/bin/logcat的完整路径,而不只是logcat

票数 21
EN

Stack Overflow用户

发布于 2011-01-20 23:34:16

这是我的工作版本:

try {
    File filename = new File(Environment.getExternalStorageDirectory()+"/logfile.log"); 
    filename.createNewFile(); 
    String cmd = "logcat -d -f "+filename.getAbsolutePath();
    Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

找到日志文件/sdcard/logfile.log

票数 25
EN

Stack Overflow用户

发布于 2013-07-17 13:46:38

   /**
     * Launches a logcat process.
     * To stop the logcat preserve the use:
     * process.destroy();
     * in the onBackPressed()
     *
     * @param filename destination of logcat output
     * @return Process logcaat is running on
     */

    public Process launchLogcat(String filename) {
        Process process = null;
        String cmd = "logcat -f " + filename + "\n";
        try {
            process = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            process = null;
        }
        return process;
    }
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3359692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档