latest posts

Had a fun time today getting a picture taken from an Android 2.3.4 HTC Vivid to my Mobile WCF Platform. Oddly enough, I could not find any tutorials on it for Monodroid like there are for MonoTouch. Piecing together several stackoverflow posts, I finally figured it out. Here is a possible solution (most likely not the best): At the top of your class, add the following:
private string _imageUri; private ImageView imageView; private Boolean isMounted {
     get {
     return Android.OS.Environment.ExternalStorageState.Equals(Android.OS.Environment.MediaMounted); }
Inside your Button Click Event:
var uri = ContentResolver.Insert(isMounted ? Android.Provider.MediaStore.Images.Media.ExternalContentUri : Android.Provider.MediaStore.Images.Media.InternalContentUri, new ContentValues()); _imageUri = uri.ToString(); var i = new Intent(Android.Provider.MediaStore.ActionImageCapture); i.PutExtra(Android.Provider.MediaStore.ExtraOutput, uri); StartActivityForResult(i, 0); ]]>
Right below your Click Event function (or anywhere inside the Activity Class you're in):
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) {
     if (resultCode == Result.Ok && requestCode == 0) {
     imageView = FindViewById(Resource.Id.ivThumbnail); imageView.DrawingCacheEnabled = true; imageView.SetImageURI(Android.Net.Uri.Parse(_imageUri)); btnUploadImage.Visibility = ViewStates.Visible; }
Then inside your "Upload Button Click" function:
Bitmap bitmap = imageView.GetDrawingCache(true); MemoryStream ms = new MemoryStream(); // Note anything less than 50 will result in very pixelated images from what I've seen bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, ms); // At this point set your Byte[] variable/property with ms.ToArray(); // for instance I have a SyncFile object with a FileData Property, so I use // SyncFile sFile = new SyncFile() {
     FileData = ms.ToArray(); }
; ]]>
Effectively this code captures a picture, puts it in an ImageView as a thumbnail on the Activity and then upon hitting your Upload Button it converts the image into a Byte Array after compressing it (or not like in my case) and from there call your WCF service upload function. Hopefully that helped someone out.