在我的应用程序中,我想从 USB 可移动存储读取文件
我有一个.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" />
如果您在棒棒糖上或之上运行,则需要在 java 代码中请求权限。
查看此链接https://stackoverflow.com/a/33162451/7457753 https://stackoverflow.com/a/33162451/7457753
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)