图库中的图像未保存到位图

image from gallery isn't saving to bitmap

我正在尝试将从画廊获得的图像保存到我的位图中,但它一直给我一个 System.NullReferenceException: 'Object reference not set to an instance of an object.' 错误。 如果我将它从我的相机拍照中保存下来,它就会工作。如果有人知道如何解决这个问题,如果你能提供帮助,我会非常高兴 <3

private void GalleryAction(object sender, DialogClickEventArgs e)
{
    Intent intent = new Intent();
    intent.SetType("image/*");
    intent.SetAction(Intent.ActionGetContent);
    StartActivityForResult(intent,1);
}
private void CameraAction(object sender, DialogClickEventArgs e)
{
    Intent intent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
    StartActivityForResult(intent, 0);
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    base.OnActivityResult(requestCode, resultCode, data);
    if (requestCode == 0)//coming from camera
    {
        if (resultCode == Result.Ok)
        {
            bitmap = (Android.Graphics.Bitmap)data.Extras.Get("data");
            iv.SetImageBitmap(bitmap);
        }
                
    }
    else if(requestCode == 1) //coming from gallery
    {
        if (resultCode == Result.Ok)
        {
            bitmap = (Android.Graphics.Bitmap)data.Extras.Get("image");
            iv.SetImageBitmap(bitmap);
        }
    }
}

使用 ACTION_GET_CONTENT 选择不会给你一个位图,而是一个所选文件的 URI。

Uri uri = data.getData)); // in Java

喜欢你提供的代码,先从 Gallery 中获取图像。然后从你选择的图像中获取uri。

然后您可以将 uri 转换为位图。

 private Android.Graphics.Bitmap NGetBitmap(Android.Net.Uri uriImage)
    {
        Android.Graphics.Bitmap mBitmap = null;
        mBitmap = Android.Provider.MediaStore.Images.Media.GetBitmap(this.ContentResolver, uriImage);
        return mBitmap;
    }

最后,你可以将位图设置为imageview。

全部代码:

public static readonly int PickImageId = 1000;
    ImageView _imageView;
    Button btn_GetImageFromGrallery;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
        _imageView = FindViewById<ImageView>(Resource.Id._imageView);
        btn_GetImageFromGrallery = FindViewById<Button>(Resource.Id.btn_GetImageFromGrallery);
        btn_GetImageFromGrallery.Click += Btn_GetImageFromGrallery_Click;
    }

    private void Btn_GetImageFromGrallery_Click(object sender, System.EventArgs e)
    {
        Intent intent = new Intent();
        intent.SetType("image/*");
        intent.SetAction(Intent.ActionGetContent);
        //StartActivityForResult(intent, 1);
        StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), PickImageId);
    }
    protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);

        if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
        {
            Android.Net.Uri uri = data.Data;

            var bitmap = NGetBitmap(uri);

            //_imageView.SetImageURI(uri);
            _imageView.SetImageBitmap(bitmap);
        };
    }
    private Android.Graphics.Bitmap NGetBitmap(Android.Net.Uri uriImage)
    {
        Android.Graphics.Bitmap mBitmap = null;
        mBitmap = Android.Provider.MediaStore.Images.Media.GetBitmap(this.ContentResolver, uriImage);
        return mBitmap;
    }