Forum Xamarin.Android


The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to Get Path of flash drive connected through OTG cable.

Ajay_SBSICAjay_SBSIC USMember ✭✭✭

I have done the following:

  1. listen to attach and detach events of usb devices
  2. getting all attached devices
  3. getting permissions for a device
    please answer me how can I get the path of Pen-drive connected through OTG cable. I am working in Xamarin.Android.
    Here is My code :

              var deviceList = mUsbManager.DeviceList;
                    IEnumerator<UsbDevice> deviceIterator = deviceList.Values.GetEnumerator();
                    while (deviceIterator.MoveNext())
                        UsbDevice device = deviceIterator.Current;
                        var mPermissionIntent = PendingIntent.GetBroadcast(this.ApplicationContext, 0, new Intent(ACTION_USB_PERMISSION), 0);
                        UsbManager mUsbManager = (UsbManager)this.GetSystemService(Context.UsbService);
                        mUsbManager.RequestPermission(device, mPermissionIntent);
                        bool perm = mUsbManager.HasPermission(device);
                        if (perm)
                            #region [path section]
                            //UsbInterface intf = device.GetInterface(0);
                            //UsbEndpoint endpoint = intf.GetEndpoint(0);
                            //UsbDeviceConnection connection = mUsbManager.OpenDevice(device);
                            //System.Boolean interfaceClaimed = connection.ClaimInterface(intf, true);
                            //int fileDescriptor = connection.FileDescriptor;
                            Java.Lang.Process process = Runtime.GetRuntime().Exec("ls /storage/UsbDriveA");
                            BufferedReader buffer = new BufferedReader(new InputStreamReader(process.InputStream));
                            string listOfFiles = "";
                            string line;
                            while ((line = buffer.ReadLine()) != null)
                                listOfFiles += line;
                            var alrt = Toast.MakeText(this as Context, "hello this is working Now" + listOfFiles.ToString() + " " + line, ToastLength.Long);
                            alrt.SetGravity(Android.Views.GravityFlags.Top, 0, 0);
                            var alrt = Toast.MakeText(this as Context, "this is else Working Now", ToastLength.Long);
                            alrt.SetGravity(Android.Views.GravityFlags.Center, 0, 0);


I am stuck in this , please someone suggest how can i get path. I just wanted to copy some Image file from pen drive, but I am not getting path of flash drive.


  • Ajay_SBSICAjay_SBSIC USMember ✭✭✭

    Is anyone going to answer this?

  • BrianKnoxBrianKnox USMember ✭✭
    edited December 2017

    I am stuck on this exact same issue. Have you found your answer yet?

  • BrianKnoxBrianKnox USMember ✭✭

    I have this partially solved now. For my device, the mount is "/mnt/usbotg".

    You can also parse this file for all mounts: "/proc/mounts".

  • SivamuralidharSivamuralidhar USMember ✭✭

    Hi Guys,

    Is this still an issue our its resolved?

    if yes can we have some solution here.

    Thank you

Sign In or Register to comment.