Android 我如何让用户在其图库中选择图片?(沙马林表格)

Android 我如何让用户在其图库中选择图片?(沙马林表格),android,xamarin.forms,Android,Xamarin.forms,我试图打开设备库,以便用户选择照片并获取其路径…但我的流始终返回null,然后,我有一个异常,因为。。。 我不知道怎么了 有人能帮我吗? 这是一个xamarin表单项目,现在我正在使用android对其进行测试 我的主要活动 [Activity(Label = "neoFly_Montana", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = Co

我试图打开设备库,以便用户选择照片并获取其路径…但我的流始终返回null,然后,我有一个异常,因为。。。 我不知道怎么了 有人能帮我吗? 这是一个xamarin表单项目,现在我正在使用android对其进行测试

我的主要活动

 [Activity(Label = "neoFly_Montana", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = false, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle bundle)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);

        //inicializa imageCircle
        ImageCircleRenderer.Init();

        //inicializa o mapa
        global::Xamarin.FormsMaps.Init(this, bundle);

        LoadApplication(new App());
    }

    // Field, property, and method for Picture Picker
    public static readonly int PickImageId = 1000;

    public TaskCompletionSource<Stream> PickImageTaskCompletionSource { set; get; }

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

        if (requestCode == PickImageId)
        {
            if ((resultCode == Result.Ok) && (intent != null))
            {
                Android.Net.Uri uri = intent.Data;
                Stream stream = ContentResolver.OpenInputStream(uri);

                // Set the Stream as the completion of the Task
                PickImageTaskCompletionSource.SetResult(stream);
            }
            else
            {
                PickImageTaskCompletionSource.SetResult(null);
            }
        }
    }
}
[活动(Label=“neoFly_Montana”,Icon=“@drawable/Icon”,Theme=“@style/MainTheme”,MainLauncher=false,ConfigurationChanges=ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
公共类MainActivity:全局::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
创建时受保护的覆盖无效(捆绑包)
{
TabLayoutResource=Resource.Layout.Tabbar;
ToolbarResource=Resource.Layout.Toolbar;
base.OnCreate(bundle);
全局::Xamarin.Forms.Forms.Init(这个,bundle);
//inicializa图像圈
ImageCirclerEnder.Init();
//马帕酒店
全局::Xamarin.formsmap.Init(这个,bundle);
加载应用程序(新应用程序());
}
//图片选择器的字段、属性和方法
公共静态只读int-PickImageId=1000;
public TaskCompletionSource PickImageTaskCompletionSource{set;get;}
受保护的覆盖void OnActivityResult(int请求代码、结果代码、意图)
{
base.OnActivityResult(请求代码、结果代码、意图);
if(requestCode==PickImageId)
{
if((resultCode==Result.Ok)&&(intent!=null))
{
Android.Net.Uri=intent.Data;
Stream=ContentResolver.OpenInputStream(uri);
//将流设置为任务的完成
PickImageTaskCompletionSource.SetResult(流);
}
其他的
{
PickImageTaskCompletionSource.SetResult(null);
}
}
}
}
在我的android项目中,我也有这样的课程: (在本例中,PicturePickerImplementation:IPICtureRepicker不起作用,IPICtureRepicker未被识别……我不知道为什么。)

using System;
using System.Collections.Generic;
using System.Linq;
 using System.Text;

 using Android.App;
 using Android.Content;
 using Android.OS;
 using Android.Runtime;
 using Android.Views;
 using Android.Widget;
 using System.Threading.Tasks;
 using System.IO;
 using Xamarin.Forms;
 using neoFly_Montana.Droid;

 [assembly: Dependency(typeof(PicturePickerImplementation))]
 namespace neoFly_Montana.Droid
 {
public class PicturePickerImplementation : IPicturePicker
{
    public Task<Stream> GetImageStreamAsync()
    {
        // Define the Intent for getting images
        Intent intent = new Intent();
        intent.SetType("image/*");
        intent.SetAction(Intent.ActionGetContent);

        // Get the MainActivity instance
        MainActivity activity = Forms.Context as MainActivity;

        // Start the picture-picker activity (resumes in MainActivity.cs)
        activity.StartActivityForResult(
            Intent.CreateChooser(intent, "Select Picture"),
            MainActivity.PickImageId);

        // Save the TaskCompletionSource object as a MainActivity property
        activity.PickImageTaskCompletionSource = new TaskCompletionSource<Stream>();

        // Return Task object
        return activity.PickImageTaskCompletionSource.Task;
    }

}
 }
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用System.Threading.Tasks;
使用System.IO;
使用Xamarin.Forms;
使用neoFly_Montana.Droid;
[程序集:依赖项(类型(实现))]
名称空间neoFly_Montana.Droid
{
公共类PicturePicturePickerImplementation:IPictureRepicker
{
公共任务GetImageStreamAsync()
{
//定义获取图像的意图
意图=新意图();
intent.SetType(“image/*”);
intent.SetAction(intent.ActionGetContent);
//获取MainActivity实例
MainActivity=Forms.Context作为MainActivity;
//启动图片选择器活动(在MainActivity.cs中恢复)
activity.StartActivityForResult(
Intent.CreateChooser(Intent,“选择图片”),
MainActivity.PickImageId);
//将TaskCompletionSource对象另存为MainActivity属性
activity.PickImageTaskCompletionSource=新的TaskCompletionSource();
//返回任务对象
返回activity.PickImageTaskCompletionSource.Task;
}
}
}
pcl中的我的接口:

public interface IPicturePicker
{
    Task<Stream> GetImageStreamAsync();
}
公共接口IPicturePicker
{
任务GetImageStreamAsync();
}

尝试完全限定IPicturePicker抱歉,我不明白…我该怎么做?您能解释一下吗?您需要告诉代码IPicturePicker位于哪个命名空间中,或者通过“使用”语句,或者通过显式包含命名空间。同意Jason的观点,尝试使用您的代码来重现问题,您的代码在我这边运行良好。尝试完全限定IPiturePicker抱歉,我不明白…我怎么做,请?您能解释一下吗?您需要通过在顶部的“using”语句,或者通过显式地包含名称空间。同意Jason的观点,尝试使用您的代码重现问题,您的代码在我这边运行良好。