I am trying to read thee content of a USB pendrive connected to the phone via otg cable. I can get the path of the pendrive but the listFiles() function returns null. No problem with the internal memory. I have specified also permissions. Why doesn't work?
// 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();