Android获取存储和打印输出Logcat日志

一、首先要把权限添加到AndroidManifest中

<!-- 读取Log权限 -->
<uses-permission android:name="android.permission.READ_LOGS" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 从SDCard读出数据权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

二、然后获取Logcat中的日志

1.首先我们要先定义一个String[]数组,里面的代码是
//第一个是Logcat ,也就是我们想要获取的log日志
//第二个是 -s 也就是表示过滤的意思
//第三个就是adb命令 我们要过滤的类型 E表示error ,我们也可以换成 D :debug, I:info,W:warm等等
详情看
String[] running = new String[]{"logcat","-s","adb logcat *: W"};
2.还需要一个process类,作用通俗来讲就是用Java代码来进行adb命令行操作代码是:
Process exec = Runtime.getRuntime().exec(running);

三、接下来开始使用IO流进行字符操作,把数据保存在Android SDCard中指定位置

开启一个线程,线程中的方法就是通过IO流先读取Logcat中的数据,然后再把数据通过OutPutStream方法写入到SDCard中

		//存储日志文件路径
		private static String filePath = "/storage/emulated/0/log/Log.txt";
        //adb命令 日志过滤条件
        String[] running = new String[]{"logcat", "-s", "adb logcat FaceSDK:E *:S"};
        try {
            Process exec = Runtime.getRuntime().exec(running);
            final InputStream is = exec.getInputStream();
            new Thread() {
                @Override
                public void run() {
                    FileOutputStream os = null;
                    try {
                        //新建一个路径信息
                        os = new FileOutputStream(filePath);
                        int len = 0;
                        byte[] buf = new byte[1024];
                        while (-1 != (len = is.read(buf))) {
                            os.write(buf, 0, len);
                            os.flush();
                        }
                    } catch (Exception e) {
                        Log.d("writelog",
                                "read logcat process failed. message: "
                                        + e.getMessage());
                    } finally {
                        if (null != os) {
                            try {
                                os.close();
                                os = null;
                            } catch (IOException e) {
                                // Do nothing
                            }
                        }
                    }
                }
            }.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

然后运行程序再打开我们的SDCard中的文件目录,这样我们就已经获取到了Logcat中的日志了

四、之后我们按行读取Txt文本中的内容

/** 
     * 根据行读取内容 
     * @return 
     */  
    public List<String> Txt() {    
        //将读出来的一行行数据使用List存储    
        String filePath = "/storage/emulated/0/log/Log.txt";    
  
        List newList=new ArrayList<String>();  
        try {    
            File file = new File(filePath);    
            int count = 0;//初始化 key值    
            if (file.isFile() && file.exists()) {//文件存在    
                InputStreamReader isr = new InputStreamReader(new FileInputStream(file));    
                BufferedReader br = new BufferedReader(isr);    
                String lineTxt = null;    
                while ((lineTxt = br.readLine()) != null) {  
                    if (!"".equals(lineTxt)) {    
                        String reds = lineTxt.split("\\+")[0];  //java 正则表达式    
                        newList.add(count, reds);  
                        count++;    
                    }    
                }    
                isr.close();    
                br.close();    
            }else {    
                Log.e("tag", "can not find file");
            }    
        } catch (Exception e) {    
            e.printStackTrace();    
        }    
        return newList;    
    }    

五、最后清空日志

/**
     * 删除Log文件
     * @param filePath 文件路径和名称
     */
    public static void delFile(String filePath){  
        File file = new File(filePath);  
        if(file.isFile()){  
            file.delete();  
        }  
        file.exists();  
    }  
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tan.]der

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值