在我的应用我想读取USB移动存储我已经A.TXT一个文件,我想读它
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" />