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); } } } 
6
  • are you sure you should be using "image" and not "data" to get the results? Commented Mar 5, 2021 at 14:43
  • "data" doesn't work either Commented Mar 5, 2021 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? Commented Mar 5, 2021 at 15:42
  • I believe that "data" should return a URI that you can then use to get the image data Commented Mar 5, 2021 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 Commented Mar 5, 2021 at 15:50

2 Answers 2

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; } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much <3333 it works perfectly!
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 

2 Comments

It Keeps giving me an error that GetData doesn't exist
You should translate that java to xamarin. Try more. Find examples.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.