Transfer Files USB Mass Storage OTG

雨燕双飞 提交于 2019-12-02 07:01:23

Well, I ended up going a totally different route with this. I am now using standard java.io.File and working my way through the directory structure. My code is not yet refined, but here is the gist of it...

private void setupFileIO() {
    File storageDir = new File("/storage");
    File files[] = storageDir.listFiles();

    File directory = null;
    if (files != null && files.length > 0) {
        for (File f : files) {
            if (f.isDirectory() && f.getPath().toLowerCase().contains("usb") && f.canRead()) {
                directory = f;
                break;
            }
        }
    }
    if (directory != null && directory.canRead()) {
        File file[] = directory.listFiles();
        if (file != null && file.length > 0) {
            for (int i = 0; i < file.length; i++) {
                String filePath = file[i].getPath();
                String extension = Files.getFileExtension(filePath);
                if (file[i].isFile() && file[i].canRead() && extension.equalsIgnoreCase("bin")) {
                    try {
                        byte[] r = Files.toByteArray(file[i]);
                        String result = BaseEncoding.base16().encode(r);
                        // do something with result
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!