-1

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");

    }         

}

  • you need runtime permissions. https://developer.android.com/training/permissions/requesting Samples are included at bottom of link. Loads of answer on SO too. – IAmGroot Jun 18 '18 at 16:45
  • I have all the permissions during runtime. i am having hard time including my code – Nandini Nadig Jun 18 '18 at 19:55
  • @NandiniNadig you still don't quite give enough information here. *What* is the error you receive? *What* did you try that didn't work? – jonathanpeppers Jun 21 '18 at 17:23
  • @Jonathan. This is the error I get: – Nandini Nadig Jun 21 '18 at 21:24
  • [ERROR] FATAL UNHANDLED EXCEPTION: Java.IO.FileNotFoundException: /mnt/media_rw/24A3-6FDF/hello.txt (Permission denied) 01-11 03:04:55.401 E/mono-rt ( 5309): at (wrapper dynamic-method) System.Object.28110580-fe63-4db0-96a8-22531e1d0520(intptr,intptr,intptr) 01-11 03:04:55.401 E/mono-rt ( 5309): --- End of managed Java.IO.FileNotFoundException stack trace --- 01-11 03:04:55.401 E/mono-rt ( 5309): java.io.FileNotFoundException: /mnt/media_rw/24A3-6FDF/hello.txt (Permission denied) 01-11 03:04:55.401 E/mono-rt ( 5309): at java.io.FileInputStream.open(Native Method) – Nandini Nadig Jun 21 '18 at 21:26
  • @NandiniNadig you should the error in the original question, and format it as a code block so people can read it. Does it work if you use the regular .NET APIs? `System.IO.File.ReadAllText`? I don't know why you need to use `Java.IO` here. – jonathanpeppers Jun 22 '18 at 14:19
  • @jonathanpeppers. I used ReadAllText and I get UNHANDLED EXCEPTION: 01-11 23:07:01.494 I/MonoDroid( 2923): System.IO.DirectoryNotFoundException: Could not find a part of the path "/mnt/media_rw/24A3-6FDF/hello.txt". Even though the file is there – Nandini Nadig Jun 22 '18 at 17:26

1 Answers1

0

Since Android M+ you need to request permission not only in Manifest but at runtime also. You can see how to do it with this link

The second this I see in your code you're using the device as USB host. You need to adjust your manifest for this. Go carefully with the android docs. You'll need one more permission and the uses-feature tag

<uses-feature android:name="android.hardware.usb.host" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • Thanks for the reply. I did have requested permissions during runtime. it says I have permissions, but gives permission denied exception. Mu code is on c# though – Nandini Nadig Jun 18 '18 at 19:49
  • The USb drive is automatically mounted on /mnt/media_rw/(USB dribe name) whic is "/mnt/media_rw/C227-875D" – Nandini Nadig Jun 18 '18 at 19:56
  • Sorry, didn't see the xamarin tag. You should google permission denied exception or add some code so people could help you faster! And mention that you DO request the permission – Rainmaker Jun 18 '18 at 20:25
  • xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.WebFormsClient" android:installLocation="auto"> – Nandini Nadig Jun 18 '18 at 20:31
  • okey, I edited my answer, I don't work with xamarin but I work with android, until there no xamarin pros here let's see if we can figure this out with the docs – Rainmaker Jun 18 '18 at 20:52
  • Xamarin.Android is just Android. So getting this to work in C# should be the same as Java, just imagine some of the letters are lowercase and C# properties are `getX` methods. @NandiniNadig you still don't quite give enough information here. *What* is the error you receive? *What* did you try that didn't work? – jonathanpeppers Jun 21 '18 at 17:23
  • Yes I am aware of that. I added all these permissions but no luck – Nandini Nadig Jun 21 '18 at 21:28
  • Saw this thread on stack flow. According to this we cannot access the mnt/media_rw/ from android 6.0 onwards? link : https://stackoverflow.com/questions/32201169/how-app-can-access-files-on-usb-otg-storages-in-android-6-0-api-level-23-witho – Nandini Nadig Jun 21 '18 at 21:58