android执行外部命令、检测文件是否存在、自动检测U盘路径

 1     private final String UDiskFileName = "/2969_logo/bootfile.image";
 2     private final String LocalFile = "/tmp/factory/bootfile.image";
 3     private boolean setBootLogo()
 4     {
 5         String tmp = getExternalStorageDirectory();
 6         String USBPath = tmp.split("\n")[0];
 7         if(USBPath.length() <= 0)
 8         {
 9             Toast.makeText(TSBPanelMode.this, "没有检测到U盘",
10                            Toast.LENGTH_LONG).show();
11             return false;
12         }
13         String UDiskFile = USBPath + UDiskFileName;
14         Log.d("XYP_DEBUG", UDiskFile);
15         if (!fileIsExists(UDiskFile))
16         {
17             Toast.makeText(TSBPanelMode.this, "U盘中没有文件" + UDiskFileName,
18                            Toast.LENGTH_LONG).show();
19             return false;
20         }
21         
22         try
23         {
24             String tmpcmd = "mount -o remount,rw /system ;\n"+
25                             "rm " + LocalFile + "; \n" +
26                             "cp " + UDiskFile + " " + LocalFile +";\n" + 
27                             "ls;\n";
28             String[] cmd = new String[] { "/bin/sh","-c", tmpcmd};
29             Runtime runtime = Runtime.getRuntime();  
30             Process proc = runtime.exec(cmd); 
31             InputStream is = proc.getInputStream();  
32             InputStreamReader isr = new InputStreamReader(is);  
33             String line;  
34             BufferedReader br = new BufferedReader(isr);  
35             while ((line = br.readLine()) != null)
36             {
37                 Log.d(TAG, line);
38             }
39             mTvManager.saveFactoryData();
40             Log.d("XYP_DEBUG", "==== have done ====");
41         }catch(Exception e)
42         {
43             e.printStackTrace();
44             return false;
45         }
46         
47         if (!fileIsExists(LocalFile))
48         {
49             Toast.makeText(TSBPanelMode.this, "U盘中文件没有复制到本地存储空间",
50                            Toast.LENGTH_LONG).show();
51             return false;
52         }
53         return true;
54     }
55     private boolean fileIsExists(String strFile)  
56     {  
57         try  
58         {  
59             File f=new File(strFile);  
60             if(!f.exists())  
61             {  
62                     return false;  
63             }  
64         }  
65         catch (Exception e)  
66         {  
67             e.printStackTrace();
68             return false;  
69         }  
70         return true;  
71     }  
72     public static String getExternalStorageDirectory(){  
73         String dir = new String();  
74         try {  
75             Runtime runtime = Runtime.getRuntime();  
76             Process proc = runtime.exec("mount");  
77             InputStream is = proc.getInputStream();  
78             InputStreamReader isr = new InputStreamReader(is);  
79             String line;  
80             BufferedReader br = new BufferedReader(isr);  
81             while ((line = br.readLine()) != null) {  
82                 if (line.contains("fuse")) {  
83                     String columns[] = line.split(" ");  
84                     if (columns != null && columns.length > 1) {  
85                         if(columns[1].contains("emulated"))
86                             continue;
87                         dir = dir.concat(columns[1] + "\n");  
88                     }  
89                 }  
90             }  
91         } catch (FileNotFoundException e) {  
92             // TODO Auto-generated catch block  
93             e.printStackTrace();  
94         } catch (IOException e) {  
95             // TODO Auto-generated catch block  
96             e.printStackTrace();  
97         }  
98         return dir;  
99     }      

 

转载于:https://www.cnblogs.com/hei-da-mi/p/4919543.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值