我正在尝试将我的应用程序的日志重定向到sdcard文件。但我没有做到这一点。我正在尝试这样的东西。
String cmd= "logcat -v time ActivityManager:W myapp:D *:* >\""+file.getAbsolutePath()+"\"";
Runtime.getRuntime().exec(cmd);
我也尝试了-f选项,但它也不起作用。
发布于 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
。
发布于 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
发布于 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;
}
https://stackoverflow.com/questions/3359692
复制相似问题