Android图片删除后断电重启后依然存在问题修改

发布时间 2023-12-12 14:16:23作者: 苏小七

adb shell sync后再断电重启就可以成功删除,代码如下:

android执行adb命令代码

  1 public static CommandResult execCommand(String command, boolean isRoot) {
  2         return execCommand(new String[]{command}, isRoot, true);
  3     }
  4 
  5 public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
  6         int result = -1;
  7         if (commands == null || commands.length == 0) {
  8             return new CommandResult(result, null, null);
  9         }
 10 
 11         Process process = null;
 12         BufferedReader successResult = null;
 13         BufferedReader errorResult = null;
 14         StringBuilder successMsg = null;
 15         StringBuilder errorMsg = null;
 16 
 17         DataOutputStream os = null;
 18         try {
 19             process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
 20             os = new DataOutputStream(process.getOutputStream());
 21             for (String command : commands) {
 22                 if (command == null) {
 23                     continue;
 24                 }
 25 
 26                 // donnot use os.writeBytes(commmand), avoid chinese charset error
 27                 os.write(command.getBytes());
 28                 os.writeBytes(COMMAND_LINE_END);
 29                 os.flush();
 30             }
 31             os.writeBytes(COMMAND_EXIT);
 32             os.flush();
 33 
 34             result = process.waitFor();
 35             // get command result
 36             if (isNeedResultMsg) {
 37                 successMsg = new StringBuilder();
 38                 errorMsg = new StringBuilder();
 39                 successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
 40                 errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
 41                 String s;
 42                 while ((s = successResult.readLine()) != null) {
 43                     successMsg.append(s);
 44                 }
 45                 while ((s = errorResult.readLine()) != null) {
 46                     errorMsg.append(s);
 47                 }
 48             }
 49         } catch (IOException e) {
 50             e.printStackTrace();
 51         } catch (Exception e) {
 52             e.printStackTrace();
 53         } finally {
 54             try {
 55                 if (os != null) {
 56                     os.close();
 57                 }
 58                 if (successResult != null) {
 59                     successResult.close();
 60                 }
 61                 if (errorResult != null) {
 62                     errorResult.close();
 63                 }
 64             } catch (IOException e) {
 65                 e.printStackTrace();
 66             }
 67 
 68             if (process != null) {
 69                 process.destroy();
 70             }
 71         }
 72         return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null
 73                 : errorMsg.toString());
 74     }
 75 
 76 public static class CommandResult {
 77 
 78         /**
 79          * result of command *
 80          */
 81         public int result;
 82         /**
 83          * success message of command result *
 84          */
 85         public String successMsg;
 86         /**
 87          * error message of command result *
 88          */
 89         public String errorMsg;
 90 
 91         public CommandResult(int result) {
 92             this.result = result;
 93         }
 94 
 95         public CommandResult(int result, String successMsg, String errorMsg) {
 96             this.result = result;
 97             this.successMsg = successMsg;
 98             this.errorMsg = errorMsg;
 99         }
100     }

拍照、录像保存后,调用以下接口

ShellUtils.execCommand("sync", false);