从USB移动存储读取文件 [英] read file from usb removable storage

查看:135
本文介绍了从USB移动存储读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想从USB可移动存储中读取文件 我有一个.txt,我想阅读

in my application I want to read a file from usb removable storage I have a.txt and i want to read it

void read() {
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
StringBuffer sb = new StringBuffer();
while (deviceIterator.hasNext()) {
    UsbDevice device = deviceIterator.next();
    String Model = device.getDeviceName();

    try {
        File file = new File(Model + "/a.txt");
        if (file.exists())
            Toast.makeText(getApplicationContext(), "Exist", Toast.LENGTH_LONG).show();
        else
            Toast.makeText(getApplicationContext(), "Not Exist", Toast.LENGTH_LONG).show();

        BufferedReader br = new BufferedReader(new FileReader(Model + "/a.txt"));
        String sCurrentLine;

        while ((sCurrentLine = br.readLine()) != null) {
            textView.append(sCurrentLine + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
    }
}
}

当检查文件存在时返回false,并且在打开和读取文件时抛出异常

when check file exists return false and when opening and reading file throw exception

java.io.filenotfoundexception :/dev/bus/001/002/a.txt: opent failed : EACCES (permission denied)

在清单中有

<uses-permission android:name="android.permission.USB_PERMISSION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_STORAGE" />

推荐答案

没有一个解决方案对我有用 我搜索并找到了这个库

none of this solution worked for me I searched and found this library

开放源代码库,无需在设备上生根即可访问Android上的USB Mass Storage设备

为我测试并工作

这篇关于从USB移动存储读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆