android otg u盘 视频教程,Android OTG U盘文件读写
最近要求对安卓平板开发时导出Excel表格到插在平板的U盘上,初步尝试发现,对U盘的文件读写只能操作Android/包名/的目录,不能直接写在根目录,不方便客户使用,于是研究了libaums的库可用是可用,但是调用其device.init() 方法后,就不能在文件管理里面看到U盘了,所以客户使用起来还是不方便,于是想到了Linux文件操作命令。
思路是先生成文件在内置存储卡中,然后使用Linux命令将文件cp或者mv到U盘根目录
复制命令
cp -r srcPath targetPath
剪切命令
mv srcPath targetPath
首先获取U盘的路径,代码如下,有返回值说明有U盘挂载,返回值为空说明U盘未挂载
public static String getUDiskRealPath() {
String filePath = "/proc/mounts";
File file = new File(filePath);
List lineList = new ArrayList<>();
InputStream inputStream =null;
try {
inputStream = new FileInputStream(file);
if (inputStream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "GBK");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = "";
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("vfat")) {
lineList.add(line);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
if (lineList.isEmpty()){
Log.i(HEAD, "getUDiskPath no usb disk ");
return "";
}
String editPath = lineList.get(lineList.size() - 1);
Log.i(HEAD,"edit path = " + editPath);
int start = editPath.indexOf("/mnt");
int end = editPath.indexOf(" vfat");
String path = editPath.substring(start, end);
return path;
}
生成你要复制或剪切到U盘的文件并获取其路径srcPath
拼接命令传入下面的执行命令方法中,其中isRooted需传入true
/**
* Execute the command.
*
* @param commands The commands.
* @param isRooted True to use root, false otherwise.
* @param isNeedResultMsg True to return the message of result, false otherwise.
* @return the single {@link CommandResult} instance
*/
public static CommandResult execCmd(final String[] commands,
final boolean isRooted,
final boolean isNeedResultMsg) {
int result = -1;
if (commands == null || commands.length == 0) {
return new CommandResult(result, null, null);
}
Process process = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
StringBuilder successMsg = null;
StringBuilder errorMsg = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec(isRooted ? "su" : "sh");
os = new DataOutputStream(process.getOutputStream());
for (String command : commands) {
if (command == null) continue;
os.write(command.getBytes());
os.writeBytes(LINE_SEP);
os.flush();
}
os.writeBytes("exit" + LINE_SEP);
os.flush();
result = process.waitFor();
if (isNeedResultMsg) {
successMsg = new StringBuilder();
errorMsg = new StringBuilder();
successResult = new BufferedReader(new InputStreamReader(process.getInputStream(),
"UTF-8"));
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream(),
"UTF-8"));
String line;
if ((line = successResult.readLine()) != null) {
successMsg.append(line);
while ((line = successResult.readLine()) != null) {
successMsg.append(LINE_SEP).append(line);
}
}
if ((line = errorResult.readLine()) != null) {
errorMsg.append(line);
while ((line = errorResult.readLine()) != null) {
errorMsg.append(LINE_SEP).append(line);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (successResult != null) {
successResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
e.printStackTrace();
}
if (process != null) {
process.destroy();
}
}
return new CommandResult(
result,
successMsg == null ? null : successMsg.toString(),
errorMsg == null ? null : errorMsg.toString()
);
}
例如
/**源文件*/
String srcPath = "/storage/emulated/0/test.txt";
/**目标位置,如U盘跟路径*/
String targetPath = getUDiskRealPath();
/**拼接复制命令*/
String cmd = "cp -r " + srcPath + " " + targetPath;
/**执行复制命令*/
execCmd(new String[]{cmd},true,true);
即可完成文件写在U盘根目录(或者其他目录)的操作。
写文件会了,读文件也就是反过来进行了,先复制或剪切文件到内置存储卡,在进行基本的文件操作即可。
标签:U盘,OTG,new,Android,null,os,line,String
来源: https://blog.csdn.net/Panda_Kill/article/details/106923326
android otg u盘 视频教程,Android OTG U盘文件读写相关推荐
- android安卓开发入门视频教程资料百度网盘下载
android安卓开发入门视频教程资料讲解安卓核心基础,包含视频+笔记,适合新手入门学习. 百度网盘:https://pan.baidu.com/s/1uciMAAa97nm5RSLILtdPdg&a ...
- 网众linux无盘视频教程,网众无盘新手篇--LINUX系统网络安装
Linux服务器安装施工前,需要准备以下工作: 1).借助windows机器为服务器网络部署Linux系统,服务器开启PXE网络启动,同时开启硬盘AHCI模式,需要使用虚拟机用户,BOIS中设置中开启 ...
- Android初级到高级视频教程全套 百度网盘下载
百度网盘视频资源下载 https://pan.baidu.com/s/1MVYKj4L8-1nU7Er3z5UGRA https://pan.baidu.com/s/1ewu5bDpuggEu9y_K ...
- android socket上传视频教程,android socket视频流方案
分两块: 1.取得摄像头采集的视频流 2.发送到服务器端 ` protected MediaRecorder mMediaRecorder; private LocalServerSocket mLs ...
- android视频快速压缩视频教程,Android上的快速视频压缩
我想在上传之前将视频文件上传到服务器并进行压缩 . 我正在使用ffmpeg libx264 . 我看过viber可以在一分钟内上传30秒大小为78MB的视频文件[降低到2.3MB] . 我想知道他们怎 ...
- android otg u盘 视频教程,手机u盘怎么用|OTG U盘正确使用教程
当今社会,U盘的种类越来越多,今天小编想跟大家分享的是手机U盘.首先我们要弄清楚什么是"手机U盘". 手机U盘就是手机U盘,全称是智能手机USB闪存驱动器,简称智能U盘-V盘,英文 ...
- Android OTG U盘文件读写
Android U盘读写要用到的三方库:https://github.com/magnusja/libaums,使用方法地那就链接了解. 最近项目需要用到OTG功能,写了一个小demo,做为自己的笔记 ...
- 【Android取证篇】华为手机OTG备份密码重置教程
[Android取证篇]华为手机OTG备份密码重置教程 提取华为设备遇到OTG备份出来的数据信息软件无法正常解析时,排除数据提取不完整.软件设备等问题,可考虑重置华为的备份密码,重新备份数据再分析 ...
- Android U盘文件读写复制操作
Android U盘文件读写复制操作 应用开发起因 一.U盘的拔插监听及读取U盘设备 二.读取文件列表 1.U盘文件列表读取 2.Storage文件列表读取 三.通过文件列表进入文件,进行文本文件的读 ...
最新文章
- 入机器学习大坑,需要什么样的数学水平?
- 简单明了!OLTP场景下的数据分布式设计原则
- 微型计算机硬件的最小配置包括,职中计算机应用基础第一章测试题及答案
- 清官谈mysql中utf8和utf8mb4区别,请使用utf8mb4
- WPF中的容器控件——canvas
- Pattern Discovery and Anomaly Detection via Knowledge Graph-学习笔记
- 测试插件-infinitest介绍
- Python较为经典的53个Python库
- java 布隆过滤器_牛逼哄哄的布隆过滤器,到底有什么用?
- linux 安装 交换分区大小,给已安装的Linux新增Swap交换分区
- ajax json的参数,java ajax json参数
- php后台+前端开发过程整理
- [linux]CentOS安装pre-built Nginx
- 公司招聘中不能说的秘密,简历只有这么发才有希望
- 计算机桌面图片查看,电脑中查看微软bing缤纷桌面中图片信息方法
- 留言板 HTML5代码
- 初始JavaScript
- java提供的对集合操作的常用方法,并集,交集,差集
- 大数据管理与应用专业总结笔记
- 开源pdf读取_Wallabag可满足您的开源读取后应用程序需求
热门文章
- [大模拟]登机牌条码
- 2023年【金属非金属矿山提升机操作】报名考试及金属非金属矿山提升机操作最新解析
- 爬虫入门之最好大学网--专项学科全国排名爬虫
- Java NIO通信框架在电信领域的实践
- Java基础-注解机制详解
- Apple Watch卡住在苹果标志界面,该怎么解决?
- Th4.6:模板全特化、偏特化(局部特化)详述
- 广工大计算机学院QG工作室,广东工业大学基础课实验教学指导委员会工作职责...
- 2023 年上海市职业院校技能大赛高职组“信息安全管理与评估”赛项样题
- el-table中的树形结构