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 }