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

     private final String UDiskFileName = "/2969_logo/bootfile.image";
private final String LocalFile = "/tmp/factory/bootfile.image";
private boolean setBootLogo()
{
String tmp = getExternalStorageDirectory();
String USBPath = tmp.split("\n")[0];
if(USBPath.length() <= 0)
{
Toast.makeText(TSBPanelMode.this, "没有检测到U盘",
Toast.LENGTH_LONG).show();
return false;
}
String UDiskFile = USBPath + UDiskFileName;
Log.d("XYP_DEBUG", UDiskFile);
if (!fileIsExists(UDiskFile))
{
Toast.makeText(TSBPanelMode.this, "U盘中没有文件" + UDiskFileName,
Toast.LENGTH_LONG).show();
return false;
} try
{
String tmpcmd = "mount -o remount,rw /system ;\n"+
"rm " + LocalFile + "; \n" +
"cp " + UDiskFile + " " + LocalFile +";\n" +
"ls;\n";
String[] cmd = new String[] { "/bin/sh","-c", tmpcmd};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(cmd);
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null)
{
Log.d(TAG, line);
}
mTvManager.saveFactoryData();
Log.d("XYP_DEBUG", "==== have done ====");
}catch(Exception e)
{
e.printStackTrace();
return false;
} if (!fileIsExists(LocalFile))
{
Toast.makeText(TSBPanelMode.this, "U盘中文件没有复制到本地存储空间",
Toast.LENGTH_LONG).show();
return false;
}
return true;
}
private boolean fileIsExists(String strFile)
{
try
{
File f=new File(strFile);
if(!f.exists())
{
return false;
}
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
public static String getExternalStorageDirectory(){
String dir = new String();
try {
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec("mount");
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
String line;
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
if (line.contains("fuse")) {
String columns[] = line.split(" ");
if (columns != null && columns.length > 1) {
if(columns[1].contains("emulated"))
continue;
dir = dir.concat(columns[1] + "\n");
}
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dir;
}
上一篇:流程控制-switch的一些要点


下一篇:Spark学习笔记