ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Android OTG U盘文件读写

2020-06-25 14:39:04  阅读:321  来源: 互联网

标签:U盘 OTG new Android null os line String


最近要求对安卓平板开发时导出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<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

    /**
     * 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有