C# 对话框中的ImageView-Xamarin

C# 对话框中的ImageView-Xamarin,c#,android,xamarin,C#,Android,Xamarin,我正在尝试将图像保存到firebase数据库中。我使用对话框保存数据。我现在想做的是,我想通过点击一个按钮来添加图片,然后从我的图库中选择图片。现在,我希望照片的路径显示在对话上,而不是图像本身。保存firebase的路径,并将图像本身检索到另一活动中的imageview中 请问这有什么样的代码吗 我想通过点击一个按钮来添加图片,然后从我的图库中选择图片 使用选择器从库中选择图像: private void BtnClick_Click(object sender, System.EventAr

我正在尝试将图像保存到firebase数据库中。我使用对话框保存数据。我现在想做的是,我想通过点击一个按钮来添加图片,然后从我的图库中选择图片。现在,我希望照片的路径显示在对话上,而不是图像本身。保存firebase的路径,并将图像本身检索到另一活动中的imageview中

请问这有什么样的代码吗

我想通过点击一个按钮来添加图片,然后从我的图库中选择图片

使用选择器从库中选择图像:

private void BtnClick_Click(object sender, System.EventArgs e)
{
    Intent intent = new Intent();
    intent.SetType("image/*");
    intent.SetAction(Intent.ActionGetContent);
    StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), 0);
}
我希望照片的路径显示在对话中,而不是图像本身。保存firebase的路径,并将图像本身检索到另一活动中的imageview中

通过使用选择器,您可以从ActivityResult上的
检索内容Url

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
     Android.Net.Uri uri = data.Data;
     string realFilePath=GetRealFilePath(uri);
     //post the realFilePath to Firebase here

     //store realFilePath locally to pass it to other activity
}
要在Firebase中存储图像路径,需要使用以下代码将内容url转换为文件的实际路径:

public string GetRealFilePath(Uri uri)
{
    var isDocumentUri = DocumentsContract.IsDocumentUri(this, uri);
    if (isDocumentUri)
    {
        string id = DocumentsContract.GetDocumentId(uri);
        string[] split = id.Split(':');
        string type = split[0];
        if (IsMediaDocument(uri))
        {
            Uri contentUri = null;
            if ("image".Equals(type))
            {
                contentUri = MediaStore.Images.Media.ExternalContentUri;
                string selection = "_id=?";
                string[] selectionArgs = new string[] { split[1] };
                string filePath = GetDataColumn(this, contentUri, selection, selectionArgs);
                return filePath;
            }
        }
    }else
    {
      //when select the file directly from gallery, `isDocumentUri` will be false.
      var contentUri = MediaStore.Images.Media.ExternalContentUri;
      var selection = "_id=?";
      string[] selectionArgs = new string[] { uri.LastPathSegment };
      string filePath = GetDataColumn(this, contentUri, selection, selectionArgs);
      return filePath;
    }
    return null;
}

public  string GetDataColumn(Context context, Uri uri, string selection,string[] selectionArgs)
{

    ICursor cursor = null;
    string column = "_data";
    string[] projection = {
        column
    };

    try
    {
        cursor = ContentResolver.Query(uri, projection, selection, selectionArgs,
                null);
        if (cursor != null && cursor.MoveToFirst())
        {
            int column_index = cursor.GetColumnIndexOrThrow(column);
            return cursor.GetString(column_index);
        }
    }
    finally
    {
        if (cursor != null)
            cursor.Close();
    }
    return null;
}

public static bool IsMediaDocument(Uri uri)
{
    return "com.android.providers.media.documents".Equals(uri.Authority);
}

这里有一些示例,介绍如何从图库中获取图像,从中可以获取路径并显示图像。