Note that there are some explanatory texts on larger screens.

plurals
  1. POHandling Bitmap loading and disposing probably
    primarykey
    data
    text
    <p>I'm a newbie to monodroid and I have two killers here I wish someone can help me to resolve: </p> <p>I have a monodrid app which uploads an image to a web service, the images comes from the device camera or the Pictures gallery.</p> <p>First thing is I use the following code to take a picture from the device camera:</p> <pre><code>var intent = new Intent(MediaStore.ActionImageCapture); var availableActivities = this.PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly); if (availableActivities != null &amp;&amp; availableActivities.Count &gt; 0) { var dir = new Java.IO.File( Android.OS.Environment.GetExternalStoragePublicDirectory( Android.OS.Environment.DirectoryPictures), "myapp"); if (!dir.Exists()) { dir.Mkdirs(); } _file = new Java.IO.File(dir, String.Format("image-{0}.jpg", Guid.NewGuid())); StaticDataHolder.ImageUri = Android.Net.Uri.FromFile(_file); intent.PutExtra(MediaStore.ExtraOutput, Android.Net.Uri.FromFile(_file)); StartActivityForResult(intent, RESULT_CAMERA_CAPTURE); } </code></pre> <p>And this code to read scaled Bitmap from StaticDataHolder.ImageUri into an ImageView control so the user can confirm weather to use the selected image or not:</p> <pre><code>Bitmap largeBitmap = null; try { image.SetImageBitmap(null); if (Bitmap != null) { Bitmap.Recycle(); Bitmap.Dispose(); } largeBitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, StaticDataHolder.ImageUri); Bitmap = GetScaledImage(largeBitmap); SetImageFromBitmap(image, Bitmap); return true; } catch (Java.Lang.OutOfMemoryError) { return false; } finally { if (largeBitmap != null) { largeBitmap.Recycle(); largeBitmap.Dispose(); } } </code></pre> <p>The second issue is when the user confirm the selected image weather its taken from the camera or picked from the gallery, I create a new Bitmap to be compressed and sent to a web service which I host using the following code:</p> <pre><code>byte[] buffer = null; MemoryStream ms = null; Bitmap bmp = null; ms = new MemoryStream(); bmp = MediaStore.Images.Media.GetBitmap(ContentResolver, StaticDataHolder.ImageUri); bmp.Compress(Bitmap.CompressFormat.Jpeg, 60, ms); buffer = ms.GetBuffer(); ms.Dispose(); bmp.Recycle(); bmp.Dispose(); </code></pre> <p>and then I pass the buffer to the web service method.</p> <p>This code will work perfectly the first time I start the app, But when I try to load a second image I get OutOfMemoryError exception and the image is not loaded.</p> <p>How i can load a Bitmap and dispose it probably so I can avoid this exception?. Please note that every step here is managed via separate activities:</p> <p>1.Take or choose a photo activity. 2.Confirm the selected photo activity. 3.Load the selected photo and send it to the web service activity.</p> <p>As far as I know the activity is disposed when finished so by default all resources used in it will also be disposed but in my case its appears to be not!.</p> <p>I have searched it for 3 days with no luck, any help would be much appreciated. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload