I am not able to read the files from the USB drive which is connected to the android device using 7.1.1. MY manifest file has all the permissions.
I get
[ERROR] FATAL UNHANDLED EXCEPTION: Java.IO.FileNotFoundException: /mnt/media_rw/24A3-6FDF/hello.txt (Permission denied)
Here is my code in main activity
public void ReadFileFromStorage()
{
System.Diagnostics.Debug.WriteLine("inside ReadFileFromStorage");
var mPermissionIntent = PendingIntent.GetBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
UsbManager usbManager = (UsbManager)GetSystemService(Context.UsbService);
var deviceList = usbManager.DeviceList;
IEnumerable<UsbDevice> deviceIterator = deviceList.Values.AsEnumerable();
if (deviceIterator.Count() > 0)
{
UsbDevice device = deviceIterator.ElementAt(0);
usbManager.RequestPermission(device, mPermissionIntent);
if (usbManager.HasPermission(device))
{
System.Diagnostics.Debug.WriteLine("HasPermission");
UsbDeviceConnection connection = usbManager.OpenDevice(device);
if( connection != null)
{
System.Diagnostics.Debug.WriteLine(" connection is not null");
Java.IO.File dir = new Java.IO.File("/mnt/media_rw/C227-875D/");
if (dir.Exists())
{
fileList = dir.ListFiles();
Toast.MakeText(Android.App.Application.Context, "exception did not occur" + "directory exists", ToastLength.Long).Show();
System.Diagnostics.Debug.WriteLine("dir exists");
if (dir.IsDirectory)
{
System.Diagnostics.Debug.WriteLine(dir.Path);
}
}
}
var model = device.DeviceName;
string inlne = null;
BufferedReader reader = null;
// read the contents of the file
reader = new BufferedReader(new FileReader("/mnt/media_rw/C227-875D/test/hello.txt"));
System.Diagnostics.Debug.WriteLine("no exception occured");
}
}