Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Xamarin Android app crashing right after taking (or selecting) a photo

$
0
0

Hi guys,

I have built a nice little Android app which overall works really well! The app allows the user to take a photo and/or select an existing photo from the phone's Gallery. In both cases, I simply store the resulting Uri of the saved and/or selected image. For photos taken with the camera, I have limited the size strictly to 1Mb, so that I'm not creating huge files.

Occasionally and intermittently, the app will crash after taking a photo with the camera OR selecting an image from the phone's gallery.

It works say maybe 60% of the time, but crashes maybe less than half the time.

For my testing I have a fairly cheap device which is a Huawei $99 Y530.

Here is the code that I am using to initiate the camera and/or gallery selection process:

        String[] items = { "Take Photo", "Choose from Library", "Cancel" };
        ImageUri = null;
        ImageNumber = 0;

        using (var dialogBuilder = new AlertDialog.Builder(this.Activity))
        {
            dialogBuilder.SetTitle("Add Photo");
            dialogBuilder.SetItems(items, (d, args) => {
                //Take photo
                if (args.Which == 0)
                {
                    if (_currentLocation != null)
                    {
                        var intent = new Intent(MediaStore.ActionImageCapture);
                        Java.IO.File _file = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(""), String.Format("NatureMapr_{0}.jpg", Guid.NewGuid()));
                        ImageUri = Android.Net.Uri.FromFile(_file);
                        intent.PutExtra(MediaStore.ExtraOutput, ImageUri);
                        intent.PutExtra(MediaStore.ExtraSizeLimit, "1048576");
                        this.StartActivityForResult(intent, REQUEST_CAMERA);
                    }
                    else
                    {
                        this.Activity.RunOnUiThread(() => Toast.MakeText(this.Activity, "A GPS signal is required!", ToastLength.Short).Show());
                    }
                }
                //Choose from gallery
                else if (args.Which == 1)
                {
                    var intent = new Intent(Intent.ActionPick, MediaStore.Images.Media.InternalContentUri);
                    intent.SetType("image/*");
                    this.StartActivityForResult(Intent.CreateChooser(intent, "Select Picture"), SELECT_FILE);
                }
            });

            dialogBuilder.Show();
        }

Any help would be greatly appreciated...


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>