0

OK. I am pulling my hair out over this. I am using the following code to produce a upload via FTP of a photo selected by the user.

            using (WebClient client = new WebClient())
            {
                client.Credentials = new NetworkCredential("[username]", "[password]");
                client.UploadFile("XXXXXXXXXX" + destinationName, "STOR", sourceFile);
            }

This being a standard .net implimentation. The problem being then source File. I have implimented a click event, along with a return event from.

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
        {

        }
    }

Here is the problem which is driving me insane. I need the physical path of the selected photo returned from the OnActiveResult event, in order for the FTP to upload properly.

I've tried;

Android.Net.Uri uri = data.Data;
physicalAddress = Convert.ToString(data.Data);
physicalAddress = Convert.ToString(uri);

None of these return the path just "content:\android.provider.media\documents\document\image%123164"

This when provided to the function above, returns an error as the provided source file doesnt exist.

Pulling hair out! please help!

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
user3516183
  • 71
  • 2
  • 11

1 Answers1

0

Here is a complete example of extracting a full path from an Image content-based Uri.

Note: This will work on KitKit and above.

[Activity(Label = "FilePathFromContentURI", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Main);
        Button button = FindViewById<Button>(Resource.Id.myButton);
        button.Click += delegate { 
            Intent intent = new Intent();
            intent.SetType("image/*");
            intent.SetAction(Intent.ActionGetContent);
            StartActivityForResult(Intent.CreateChooser(intent, "Select Image"), 1);
        };
    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);

        ICursor cursor = null;
        try
        {
            // assuming image
            var docID = DocumentsContract.GetDocumentId(data.Data);
            var id = docID.Split(':')[1];
            var whereSelect = MediaStore.Images.ImageColumns.Id + "=?";
            var projections = new string[] { MediaStore.Images.ImageColumns.Data };
            // Try internal storage first
            cursor = ContentResolver.Query(MediaStore.Images.Media.InternalContentUri, projections, whereSelect, new string[] { id }, null);
            if (cursor.Count == 0)
            {
                // not found on internal storage, try external storage
                cursor = ContentResolver.Query(MediaStore.Images.Media.ExternalContentUri, projections, whereSelect, new string[] { id }, null);
            }
            var colData = cursor.GetColumnIndexOrThrow(MediaStore.Images.ImageColumns.Data);
            cursor.MoveToFirst();
            var fullPathToImage = cursor.GetString(colData);
            Log.Info("MediaPath", fullPathToImage);
        }
        catch (Error err)
        {
            Log.Error("MediaPath", err.Message);
        }
        finally
        {
            cursor?.Close();
            cursor?.Dispose();
        }
    }
}
SushiHangover
  • 73,120
  • 10
  • 106
  • 165