Unauthorized access while copying files onto USB via OTG

SushilTripathi.9693SushilTripathi.9693 ✭✭USMember ✭✭
edited July 2016 in Xamarin.Android

I am writing a code to paste files onto the USB Sandisk pen drive connected to my android KitKat device using an OTG cable.

I am using the following code snippet.

Using the following code I get a popup asking permission and on allowing it the mUsbManager.HasPermission(device); returns true, but while i copy the file I get an Unauthorized access exception.

I have set the WRITE_EXTERNAL_STORAGE permission.

//Code Snippet

UsbManager manager = (UsbManager)_mainActivity.GetSystemService(Context.UsbService);
var deviceList = manager.DeviceList;
IEnumerable<UsbDevice> deviceIterator = deviceList.Values.AsEnumerable();

if (deviceIterator.Count() > 0)
{
    var device = deviceIterator.ElementAt(0);

    ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    var mPermissionIntent = PendingIntent.GetBroadcast(_mainActivity.ApplicationContext, 0, new Intent(ACTION_USB_PERMISSION), 0);

    UsbManager mUsbManager = (UsbManager)_mainActivity.GetSystemService(Context.UsbService);
    mUsbManager.RequestPermission(device, mPermissionIntent);

    bool perm = mUsbManager.HasPermission(device);
    if (perm)
    {
        string FileNameSource = "/storage/emulated/0/FileNotPresent.txt";
        string FileNameDestination = "/storage/2A85-5687/FileNotPresent.txt"; 

        File.Copy(FileNameSource, FileNameDestination);
    }
}

Second question how can i get the path for the connected USB that as of now i have hard coded into the FileNameDestination variable.

A sample code snippet will really be helpful. Thanks.

Posts

  • BrianKnoxBrianKnox ✭✭ USMember ✭✭

    I am stuck on this same issue, except that I am trying to figure out how to copy FROM the USB drive to the internal storage. When I query the disk mounts for all available locations, the jump drive is not showing in this list; but I can see it from File Manager, so I know the OTG is working.

    Here's the calls we use to find the various disk mounts:

    {mainActivity}.GetExternalFilesDir()

    And then to top that off, we simply hardcode the full list of base paths that we've seen on Android devices, and we navigate to each of these paths with "Directory.Exists()".

            _FindExternalSDCards("/storage/");
            _FindExternalSDCards("/storage/external/"); // nokomis449's device was like this.
    
            _FindExternalSDCards("/Removable/");
    
            _FindExternalSDCards("/mnt/");
            _FindExternalSDCards("/mnt/sdcard/");
            _FindExternalSDCards("/mnt/sdcard/extStorages/");
    

    But with all of this code, we still don't find the USB jump drive! The results we get from this code is the SAME whether we have the OTG jump drive connected or not.

    What are we missing?

  • BrianKnoxBrianKnox ✭✭ USMember ✭✭

    UPDATE: We found the path; it was "/mnt/usbotg".

    Also, you can read the available paths from "proc/mounts" file.

    This only solves the issue of "finding" the path, but not the issue of failed write-access.

Sign In or Register to comment.