在我的应用我想读取USB移动存储 我有A.TXT一个文件,我想读它读取USB移动存储文件
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并打开时,和阅读文件抛出异常
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" />
那是什么型号 –
检查出你的路径尝试登录它。与存在的路径进行比较应该是完全一样的 –
'/ dev/bus/001/002/a.txt'。这是一个不可能的非存在文件系统路径。哪个文件:exists()已经告诉你。您应该返回并停止使用您的代码。现在你继续,好像什么都没有发生。 – greenapps