当前位置: 首页 > 面试题库 >

将Logcat保存到Android设备中的文本文件

叶阳
2023-03-14
问题内容

我在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");


 类似资料:
  • 问题内容: 我在所有技术术语上都不是很好,所以我会尽力解释我的问题。 我已经编写了一个小脚本来打开android SDK并检查连接的设备(使用Windows 10和python 2.7.14)。我得到的代码如下: 一切正常,但我想将最后3行保存到文本文件中。我尝试过使用并将其全部转换为字符串并将其写入文件并关闭它,但是它不起作用。它甚至都没有创建文件,更不用说向它写入任何内容了。 我可能缺少一些关

  • 问题内容: 我想知道如何将PHP变量保存到txt文件,然后再次检索它们。 例: 有一个输入框,提交后,在输入框中写入的内容将保存到文本文件中。稍后,需要将结果作为变量返回。因此,可以说变量是$ text,我需要将其保存到文本文件中并能够再次取回它。 问题答案: 这应该可以执行您想要的操作,但是如果没有更多上下文,我无法确定。 将$ text写入文件: 再次检索它:

  • 目前,我的(使用的是Stackoverflow,我自己没有完整地编写)代码如下所示: 我得到错误:“打开失败;EACCES(权限被拒绝)”。 我的舱单是这样的:

  • 问题内容: 假设我有一个如下所示的javascript对象: 我将其字符串化以转换为JSON。如何将此JSON保存到本地文本文件,以便可以在记事本等中打开它。 问题答案: Node.js: 浏览器(webapi):

  • 我尝试用以下代码保存从internet下载的文件 但在运行时,我得到的错误如下 03-04 20:42:51.080 8972-8972/com.example.me.demo2 E/BitmapFactory:无法解码流:java.io.FileNotFoundExcoop: /storage/emulated/0/.tanks/4a100abb-0e55-4062-8c37-f11f4189e