Xamarin从图库中选择图像路径是空的

时间:2014-10-27 22:17:26

标签: android xamarin xamarin.android android-gallery xamarin.forms

使用此配方尝试从库中选择一个图像,然后将其上传到s3但是 我的路径总是返回null。

private string _imgPath;
        public void InitializeMediaPicker()
        {
                Intent = new Intent();
                Intent.SetType("image/*");
                Intent.SetAction(Intent.ActionGetContent);
                StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), 1000);
        }

        public string GetImage()
        {
            InitializeMediaPicker();

            return _imgPath;
        }

        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if ((requestCode != 1000) || (resultCode != Result.Ok) || (data == null)) return;
            var uri = data.Data;
            _imgPath = GetPathToImage(uri);
        }

        private string GetPathToImage(Android.Net.Uri uri)
        {
            string path = null;
            // The projection contains the columns we want to return in our query.
            var projection = new[] { Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data };
            using (var cursor = ManagedQuery(uri, projection, null, null, null))
            {
                if (cursor == null) return path;
                var columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data);
                cursor.MoveToFirst();
                path = cursor.GetString(columnIndex);
            }
            return path;
        }

2 个答案:

答案 0 :(得分:12)

以下是移植自this question,特别是this answer.

的有效实施方案

在清单中包含WRITE_EXTERNAL_STORAGE权限,以使此代码生效。

public delegate void OnImageResultHandler(bool success, string imagePath);

protected OnImageResultHandler _imagePickerCallback;
public void GetImage(OnImageResultHandler callback)
{
    if (callback == null) {
        throw new ArgumentException ("OnImageResultHandler callback cannot be null.");
    }

    _imagePickerCallback = callback;
    InitializeMediaPicker();
}

public void InitializeMediaPicker()
{
    Intent = new Intent();
    Intent.SetType("image/*");
    Intent.SetAction(Intent.ActionGetContent);
    StartActivityForResult(Intent.CreateChooser(Intent, "Select Picture"), 1000);
}

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    if ((requestCode != 1000) || (resultCode != Result.Ok) || (data == null)) {
        return;
    }

    string imagePath = null;
    var uri = data.Data;
    try {
        imagePath = GetPathToImage(uri);
    } catch (Exception ex) {
        // Failed for some reason.
    }

    _imagePickerCallback (imagePath != null, imagePath);
}

private string GetPathToImage(Android.Net.Uri uri)
{
    string doc_id = "";
    using (var c1 = ContentResolver.Query (uri, null, null, null, null)) {
        c1.MoveToFirst ();
        String document_id = c1.GetString (0);
        doc_id = document_id.Substring (document_id.LastIndexOf (":") + 1);
    }

    string path = null;

    // The projection contains the columns we want to return in our query.
    string selection = Android.Provider.MediaStore.Images.Media.InterfaceConsts.Id + " =? ";
    using (var cursor = ManagedQuery(Android.Provider.MediaStore.Images.Media.ExternalContentUri, null, selection, new string[] {doc_id}, null))
    {
        if (cursor == null) return path;
        var columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data);
        cursor.MoveToFirst();
        path = cursor.GetString(columnIndex);
    }
    return path;
}

使用示例:

Button button = FindViewById<Button> (Resource.Id.myButton);

button.Click += delegate {
    GetImage(((b, p) => {
        Toast.MakeText(this, "Found path: " + p, ToastLength.Long).Show();
    }));
};

我使用了一个回调而不是返回GetImage的路径,因为调用方法会在调用OnActivityResult之前完成执行,因此返回的路径永远不会有效。

答案 1 :(得分:5)

我将Xamarin recipe中的GetPathToImage方法更改为下面的代码,现在它可以正常工作了!

private string GetPathToImage(Android.Net.Uri uri)
    {
        ICursor cursor = this.ContentResolver.Query(uri, null, null, null, null);
        cursor.MoveToFirst();
        string document_id = cursor.GetString(0);
        document_id = document_id.Split(':')[1];
        cursor.Close();

        cursor = ContentResolver.Query(
        Android.Provider.MediaStore.Images.Media.ExternalContentUri,
        null, MediaStore.Images.Media.InterfaceConsts.Id + " = ? ", new String[] { document_id }, null);
        cursor.MoveToFirst();
        string path = cursor.GetString(cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Data));
        cursor.Close();

        return path;
    }