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

y_SBSICy_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;
                            }
                            #endregion
    
                            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);
                            alrt.Show();
                        }
                        else
                        {
                            var alrt = Toast.MakeText(this as Context, "this is else Working Now", ToastLength.Long);
                            alrt.SetGravity(Android.Views.GravityFlags.Center, 0, 0);
                            alrt.Show();
                        }
        }
    

    }

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.

Answers

  • y_SBSICy_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".

Sign In or Register to comment.