0

im trying to save the image i get from the gallery to my bitmap but it keeps giving me a System.NullReferenceException: 'Object reference not set to an instance of an object.' error. it works if i save it from taking a picture from my camera. if anyone knows how to fix the problem i'd be more than happy if you can help <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);
        }
    }
}

Jonathan
  • 15
  • 3
  • are you sure you should be using "image" and not "data" to get the results? – Jason Mar 05 '21 at 14:43
  • "data" doesn't work either – Jonathan Mar 05 '21 at 15:41
  • are you sure what is returned by the Intent is a Bitmap object? Have you tried breaking up the `Get` and the `(Bitmap)` cast into separate lines to determine which is responsible for the null? – Jason Mar 05 '21 at 15:42
  • I believe that "data" should return a URI that you can then use to get the image data – Jason Mar 05 '21 at 15:44
  • i need to use bitmap because thats how i store images on my database. and i dont know how to convert URIs into bitmap – Jonathan Mar 05 '21 at 15:50
  • https://stackoverflow.com/questions/3879992/how-to-get-bitmap-from-an-uri – Jason Mar 05 '21 at 15:51

2 Answers2

1

Like the code you provided to piack the image from Grallery first. And then get the uri from the image which you picked.

Then you could convert the uri to 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;
    }

And the end, you could set the bitmap to imageview.

The whole code:

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;
    }
Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
0

Selecting with ACTION_GET_CONTENT does not give you a bitmap but an Uri of the choosen file.

Uri uri = data.getData)); // in Java
blackapps
  • 8,011
  • 2
  • 11
  • 25