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

该博客主要介绍如何在Android中检测U盘,并执行外部命令来操作文件。通过`getExternalStorageDirectory()`获取U盘路径,使用`fileIsExists()`检查文件存在性,然后执行shell命令进行文件操作。如果在U盘中找不到所需文件,或者无法复制到本地存储,程序会显示相应提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

}

golang os&sol;exec 执行外部命令

exec包执行外部命令,它将os.StartProcess进行包装使得它更容易映射到stdin和stdout,并且利用pipe连接i/o. func LookPath(file string) (st ...

PHP在linux上执行外部命令

PHP在linux上执行外部命令 一.PHP中调用外部命令介绍二.关于安全问题三.关于超时问题四.关于PHP运行linux环境中命令出现的问题 一.PHP中调用外部命令介绍在PHP中调用外部命令,可以 ...

Android执行shell命令

一.方法 /** * 执行一个shell命令,并返回字符串值 * * @param cmd * 命令名称&参数组成的数组(例如:{"/system/bin/cat", &q ...

Powershell 执行外部命令

Powershell 执行外部命令 724 11月, 2011  在 Powershell  tagged Powershell教程 / 程序 by Mooser Lee本文索引[隐藏]1通过nets ...

php -- PHP在linux上执行外部命令,system&lpar;&rpar;&comma;exec&lpar;&rpar;&comma;shell&lowbar;exec&lpar;&rpar;

目录:一.PHP中调用外部命令介绍二.关于安全问题三.关于超时问题四.关于PHP运行linux环境中命令出现的问题 一.PHP中调用外部命令介绍 在PHP中调用外部命令,有三种方法: 1. 调用专门函 ...

php执行外部命令

php执行外部命令的不少,例如:exec.shell_exec.system.popen等.我特意的讲一下popen,它跟其他函数不太一样,其他函数执行一个命令后,会等待其返回后,再向下执行,而pop ...

Android执行shell命令 top ps

Android执行shell命令 一.方法 /** * 执行一个shell命令,并返回字符串值 * * @param cmd * 命令名称&参数组成的数组(例如:{"/system/ ...

php在linux中执行外部命令

目录:一.PHP中调用外部命令介绍二.关于安全问题三.关于超时问题四.关于PHP运行linux环境中命令出现的问题 一.PHP中调用外部命令介绍在PHP中调用外部命令,可以用,1>调用专门函数. ...

PHP执行外部命令【转】

PHP是完全支持外部命令的,但是出于安全考虑,一般很少使用. PHP提供共了3种方法调用外部命令: (1)调用执行外部命令函数(system(),exec(),passthru(),shell_exe ...

随机推荐

用PHP链接mysql数据库

PHP提供了两套数据库可用于访问mysql数据库 1)MySQL扩展函数数据库 2)MySQLI扩展数据库(improved) 使用MySQLI函数访问MySQL数据库步骤 1)链接数据库管理系统 m ...

安装Postman

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值