在 Android 设备上运行应用程序时,我发现了一些崩溃情况,这些情况在模拟器中没有显示。因此,我需要将 Logcat 保存到设备内存或 SD 卡的文本文件中。请问您有什么好的方法可以做到这一点吗?
在 Android 设备上运行应用程序时,我发现了一些崩溃情况,这些情况在模拟器中没有显示。因此,我需要将 Logcat 保存到设备内存或 SD 卡的文本文件中。请问您有什么好的方法可以做到这一点吗?
在您的应用程序开头使用一个 Application 类。这将允许进行适当的文件和日志处理。
下面的代码将在以下位置创建一个日志文件:
/ExternalStorage/MyPersonalAppFolder/logs/logcat_XXX.txt
XXX是当前时间的毫秒数。每次运行您的应用程序,都会创建一个新的logcat_XXX.txt文件。
public class MyPersonalApp extends Application {
/**
* Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
*/
public void onCreate() {
super.onCreate();
if ( isExternalStorageWritable() ) {
File appDirectory = new File( Environment.getExternalStorageDirectory() + "/MyPersonalAppFolder" );
File logDirectory = new File( appDirectory + "/logs" );
File logFile = new File( logDirectory, "logcat_" + System.currentTimeMillis() + ".txt" );
// create app folder
if ( !appDirectory.exists() ) {
appDirectory.mkdir();
}
// create log folder
if ( !logDirectory.exists() ) {
logDirectory.mkdir();
}
// clear the previous logcat and then write the new one to the file
try {
Process process = Runtime.getRuntime().exec("logcat -c");
process = Runtime.getRuntime().exec("logcat -f " + logFile);
} catch ( IOException e ) {
e.printStackTrace();
}
} else if ( isExternalStorageReadable() ) {
// only readable
} else {
// not accessible
}
}
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if ( Environment.MEDIA_MOUNTED.equals( state ) ) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if ( Environment.MEDIA_MOUNTED.equals( state ) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals( state ) ) {
return true;
}
return false;
}
}
您需要在.manifest文件中正确设置应用程序类的名称和权限:
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".MyPersonalApp"
... >
编辑:
如果你想要保存特定活动的日志记录..
替换:
process = Runtime.getRuntime().exec("logcat -f " + logFile);
使用:
process = Runtime.getRuntime().exec( "logcat -f " + logFile + " *:S MyActivity:D MyActivity2:D");
adb shell logcat -t 500 > D:\logcat_output.txt
打开终端/命令提示符并导航到带有adb的文件夹,如果它还没有添加到您的环境变量中,请粘贴此命令。
t是您需要查看的行数
D:\logcat_output.txt是您的logcat将存储的位置。
在你的类中使用logcat命令时,请使用-f选项:
Runtime.getRuntime().exec("logcat -f" + " /sdcard/Logcat.txt");
这将把日志转储到存储设备上的文件中。
请注意,路径“/sdcard/”可能并非所有设备都可用。您应使用标准API来访问外部存储。
由于我还不能评论,所以我将其发布为答案。
我按照 @HeisenBerg 的说法操作,对我来说很好用,但自从 Android 6.0 开始,我们必须在运行时请求权限,因此我不得不添加以下内容:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
}
并调用
process = Runtime.getRuntime().exec("logcat -f " + logFile);
只有在回调函数onRequestPermissionsResult
中
显然,在最新版本的Android中,android.permission.READ_LOGS只授予系统应用程序。
/Android/data/com.your.app/files/logs/logcat_XXX.txt
import android.app.Application
import java.io.File
import java.io.IOException
class MyApplication : Application() {
override fun onCreate() {
super.onCreate()
getExternalFilesDir(null)?.let { publicAppDirectory -> // getExternalFilesDir don't need storage permission
val logDirectory = File("${publicAppDirectory.absolutePath}/logs")
if (!logDirectory.exists()) {
logDirectory.mkdir()
}
val logFile = File(logDirectory, "logcat_" + System.currentTimeMillis() + ".txt")
// clear the previous logcat and then write the new one to the file
try {
Runtime.getRuntime().exec("logcat -c")
Runtime.getRuntime().exec("logcat -f $logFile")
} catch (e: IOException) {
e.printStackTrace()
}
}
}
}
<application
android:name=".MyApplication"
... >
添加清单权限:
uses-permission android:name="android.permission.READ_LOGS"
private static final String COMMAND = "logcat -d -v time";
public static void fetch(OutputStream out, boolean close) throws IOException {
byte[] log = new byte[1024 * 2];
InputStream in = null;
try {
Process proc = Runtime.getRuntime().exec(COMMAND);
in = proc.getInputStream();
int read = in.read(log);
while (-1 != read) {
out.write(log, 0, read);
read = in.read(log);
}
}
finally {
if (null != in) {
try {
in.close();
}
catch (IOException e) {
// ignore
}
}
if (null != out) {
try {
out.flush();
if (close)
out.close();
}
catch (IOException e) {
// ignore
}
}
}
}
public static void fetch(File file) throws IOException {
FileOutputStream fos = new FileOutputStream(file);
fetch(fos, true);
}