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

Out of memory when loading picture taken

$
0
0

I'm building an app where I need to take a picture and display it.
I followed this tutorial to get me started: take_a_picture_and_save_using_camera_app.

However I'm having a weird problem, the first time I take a picture it works fine.

When I take a picture for the second time I get a Out of memory exception when it tries to display it.

Here is my code to start the camera intent:

_cropButton.Click += (sender, args) =>
{
    var intent = new Intent(MediaStore.ActionImageCapture);
    var availableActivies = PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
    if (availableActivies != null && availableActivies.Count > 0)
    {
        var dir = new Java.IO.File(Android.OS.Environment.GetExternalStoragePublicDirectory(
                                    Android.OS.Environment.DirectoryPictures), "ColorProto2");
        if (!dir.Exists())
        {
            dir.Mkdirs();
        }
        _file = new Java.IO.File(dir, string.Format("CP2{0}.jpg", DateTime.Now.Ticks));
        intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(_file));
        StartActivityForResult(intent,100);
    }
};

Here is the code I use to display the image:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
    try
    {
        if (requestCode == 100 && resultCode == Result.Ok)
        {
            if (_file != null)
            {
                var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
                var contentUri = Android.Net.Uri.FromFile(_file);
                mediaScanIntent.SetData(contentUri);
                SendBroadcast(mediaScanIntent);
                using (Bitmap bmp = BitmapFactory.DecodeFile(_file.AbsolutePath))//MediaStore.Images.Media.GetBitmap(ContentResolver, contentUri))
                {
                    _usedImageView.SetImageBitmap(bmp);
                    _file = null;
                }
            }
        }
    }
    catch (Exception excep)
    {
        System.Diagnostics.Debug.WriteLine(excep.Message);
    }
}

Can anyone help my with solving this so I can take new pictures if I want to and display them.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



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