我正在尝试阅读通过otg电缆连接到手机的USB笔式驱动器的内容。我可以获得pendrive的路径,但listFiles()函数返回null。内部存储器没有问题。我还指定了权限。为什么不起作用?
// PERMISSIONS
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.usb.host"/>
<uses-permission android:name="android.permission.USB_PERMISSION"/>
// GET PENDRIVE PATH
UsbManager usbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
Map<String, UsbDevice> devices = usbManager.getDeviceList();
Map.Entry<String,UsbDevice> entry = devices.entrySet().iterator().next();
UsbDevice usbDevice = entry.getValue();
//PENDRIVE PATH
String path = usbDevice.getDeviceName();
File directory = new File(path);
File[] files = directory.listFiles(); // THIS IS NULL
Toast.makeText(getApplicationContext(), ""+files, Toast.LENGTH_SHORT).show();
//TRYING TO ITERATE ON A NULL OBJECT
for (File f : files)
Toast.makeText(getApplicationContext(), f.getName(), Toast.LENGTH_SHORT).show();