获取图像的本地存储路径来自"内容:"基于Uri

时间:2016-06-10 14:51:21

标签: c# android xamarin ftp xamarin.android

行。我把头发拉过来。我使用以下代码通过FTP生成用户选择的照片上传。

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

这是标准的.net实施。问题是源文件。我已经点击了一个点击事件,以及来自。的回复事件。

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

        }
    }

这是让我疯狂的问题。我需要从OnActiveResult事件返回的所选照片的​​物理路径,以便正确上传FTP。

我已经尝试过了;

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

这些都没有返回路径"内容:\ android.provider.media \ documents \ document \ image%123164"

当提供给上面的函数时,由于提供的源文件不存在而返回错误。

拔头发!请帮忙!

1 个答案:

答案 0 :(得分:0)

以下是从基于图像内容的Uri中提取完整路径的完整示例。

注意:这适用于KitKit及以上版本。

[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();
        }
    }
}