Android OTG U盘文件读写

Android OTG U盘文件读写,第1张

概述最近要求对安卓平板开发时导出Excel表格到插在平板的U盘上,初步尝试发现,对U盘的文件读写只能 *** 作Android/包名/的目录,不能直接写在根目录,不方便客户使用,于是研究了libaums的库可用是可用,但是调用其device.init()方法后,就不能在文件管理里面看到U盘了,所以客户使用起来还是不

最近要求对安卓平板开发时导出Excel表格到插在平板的U盘上,初步尝试发现,对U盘的文件读写只能 *** 作AndroID/包名/的目录,不能直接写在根目录,不方便客户使用,于是研究了libaums的库可用是可用,但是调用其device.init() 方法后,就不能在文件管理里面看到U盘了,所以客户使用起来还是不方便,于是想到了linux文件 *** 作命令。

@H_404_7@思路是先生成文件在内置存储卡中,然后使用linux命令将文件cp或者mv到U盘根目录复制命令@H_404_7@cp -r srcPath targetPath剪切命令@H_404_7@mv srcPath targetPath

首先获取U盘的路径,代码如下,有返回值说明有U盘挂载,返回值为空说明U盘未挂载

@H_404_7@ public static String getUdiskRealPath() { String filePath = "/proc/mounts"; file file = new file(filePath); List<String> 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

@H_404_7@ /** * 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() ); }

例如

@H_404_7@/**源文件*/String srcPath = "/storage/emulated/0/test.txt";/**目标位置,如U盘跟路径*/String targetPath = getUdiskRealPath();/**拼接复制命令*/String cmd = "cp -r " + srcPath + " " + targetPath;/**执行复制命令*/execCmd(new String[]{cmd},true,true);

即可完成文件写在U盘根目录(或者其他目录)的 *** 作。

@H_419_45@写文件会了,读文件也就是反过来进行了,先复制或剪切文件到内置存储卡,在进行基本的文件 *** 作即可。 总结

以上是内存溢出为你收集整理的Android OTG U盘文件读写全部内容,希望文章能够帮你解决Android OTG U盘文件读写所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1059338.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存