Jarred Capellman / Random Texels Manipulating 1s and 0s since 1995

8Feb/120

Getting a picture from Android to a WCF Service

Posted by Jarred Capellman

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<ImageView>(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.

15Jan/120

jcBENCH Android Port Released

Posted by Jarred Capellman

After some additional work getting used to the XAML-ish layout I'm done with the initial jcBENCH Android port. You can download it from here. You will need Android 2.2 or higher for it to run.

jcBENCH Android

I've only tested this on a Dual 1.2ghz HTC Vivid. However, the results were interesting. Comparing single-threaded and multi-threaded operations was curious. Running it in multi-threaded mode was actually 3 times slower. I'm not sure if the way the Task Parallel Library was implemented on Monodroid was done poorly or if there is a bug in the detection for how many cores/cpus there are inside the Mono implementation or not, but something isn't right. Single threaded versus my HTC Titan 1.5ghz SnapDragon it lost out by ~23%. Which makes sense, 300mhz difference or 20% comparing single cores to each other.

All that being said I'm content with jcBENCH for the moment until I hear feedback or come up with more features to add.

15Jan/120

apk on IIS

Posted by Jarred Capellman

Those that have seen an IIS 404 error on a file that is known to exist will know right off the bat, a lack of mime type.

A quick trip to Google found it, but for posterity's sake:

application/vnd.android.package-archive

Tagged as: , , No Comments