Transfer Files USB Mass Storage OTG

后端 未结 1 1484
暗喜
暗喜 2021-01-23 21:21

I have an external mass storage device connected to an Android device. There are several .BIN files in the root directory that I need to read into my app. I am able to connect

相关标签:
1条回答
  • 2021-01-23 21:54

    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();
                        }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题