我是靠谱客的博主 自由书包,这篇文章主要介绍Android获取存储和打印输出Logcat日志一、首先要把权限添加到AndroidManifest中二、然后获取Logcat中的日志三、接下来开始使用IO流进行字符操作,把数据保存在Android SDCard中指定位置四、之后我们按行读取Txt文本中的内容五、最后清空日志,现在分享给大家,希望可以做个参考。

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

复制代码
1
2
3
4
5
6
7
8
9
<!-- 读取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
2
3
4
5
6
7
8
9
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中

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//存储日志文件路径 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文本中的内容

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/** * 根据行读取内容 * @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; }

五、最后清空日志

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
/** * 删除Log文件 * @param filePath 文件路径和名称 */ public static void delFile(String filePath){ File file = new File(filePath); if(file.isFile()){ file.delete(); } file.exists(); }

最后

以上就是自由书包最近收集整理的关于Android获取存储和打印输出Logcat日志一、首先要把权限添加到AndroidManifest中二、然后获取Logcat中的日志三、接下来开始使用IO流进行字符操作,把数据保存在Android SDCard中指定位置四、之后我们按行读取Txt文本中的内容五、最后清空日志的全部内容,更多相关Android获取存储和打印输出Logcat日志一、首先要把权限添加到AndroidManifest中二、然后获取Logcat中的日志三、接下来开始使用IO流进行字符操作,把数据保存在Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部