2

I'm currently developing a camera application for Android on which some problems have occurred. I need it to work on all Android devices and since all of these works in different ways specially with the camera hardware, I'm having a hard time finding a solution that works for every device.

My application main goal is to launch the camera on a button click, take a photo and upload it to a server. So I don't really need the functionality of saving the image on the device, but if that's needed for further image use I might as well allow it.

For example I'm testing my application on a Samsung Galaxy SII and a Motorola Pad. I got working code that launches the camera, which is by the way C# code since I'm using Monodroid:

Intent cameraIntent = new Intent(Android.Provider.MediaStore.ActionImageCapture);  
StartActivityForResult(cameraIntent, PHOTO_CAPTURE);

And I fetch the result, similar to this guide I followed: http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/ Why I followed this guide is because the activity returns null on my galaxy device (Another device oriented problem).

This code works fine on the Galaxy device. It takes a photo and saves the photo in the gallery from which i can upload to a server. By further research this is apparently galaxy standard behaviour, so this doesn't work on my Motorola pad. The camera works fine, but no image is saved to gallery.

So with this background my question is, am I on the right path here? Do I need to save the image to gallery in order for further use in my application? Is there any solution that works for every Android device, cause that's the solution i need.

Thanks for any feedback!

Lucas Arrefelt
  • 3,879
  • 5
  • 41
  • 71

2 Answers2

1

After reading the linked article, the approach taken in that article is geared toward the Galaxy line, since they appear to write to the gallery automatically.

This article discusses some other scenarios in detail:

Android ACTION_IMAGE_CAPTURE Intent

So, I don't necessarily think that following the linked article that you provided is the right path. Not all devices automatically write to the gallery as described in that article, afaik. The article I linked to points to the issues being related to security and suggests writing the image to a /sdcard/tmp folder for storing the original image. Going down a similar path would more than likely lead to code that is going to work reliably across many devices.

Here are some other links for reference:

Google discussion regarding this subject: http://code.google.com/p/android/issues/detail?id=1480 Project with potential a solution to the problem: https://github.com/johnyma22/classdroid

While that discussion/project are in Java/Android SDK, the same concepts should apply to Monodroid. I'd be happy to help you adapt the code to a working Mono for Android solution if you need help.

Community
  • 1
  • 1
long2know
  • 1,280
  • 10
  • 9
1

To long2know: Yes the same concepts applies to Monodroid. I've already read the stack article you linked among with some other similar. However i don't like the approach in that particular post since it checks for bugs for some devices that are hardcoded into a collection. Meaning it might fail to detect bugs in future devices. Since i won't be doing maintenance on this application, i can't allow this. I found a solution elsewhere and adapted it to my case and i'll post it below if someone would need it. It works on both my devices, guessing it would work for the majority of other devices. Thanks for your post!

Solution that allows you to snap a picture and use, also with the option of using a image from gallery. Solution uses option menu for these purposes, just for testing. (Monodroid code).

Camera code is inspired by: access to full resolution pictures from camera with MonoDroid

namespace StackOverFlow.UsingCameraWithMonodroid
{
    [Activity(Label = "ImageActivity")]
    public class ImageActivity
        private readonly static int TakePicture = 1;
        private readonly static int SelectPicture = 2;
        private string imageUriString;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            this.SetContentView(Resource.Layout.ImageActivity);
        }

        public override bool OnCreateOptionsMenu(IMenu menu)
        {
            MenuInflater flate = this.MenuInflater;
            flate.Inflate(Resource.Menu.ImageMenues, menu);

            return base.OnCreateOptionsMenu(menu);
        }

        public override bool OnOptionsItemSelected(IMenuItem item)
        {
            switch (item.ItemId)
            {
                case Resource.Id.UseExisting:
                    this.SelectImageFromStorage();
                    return true;
                case Resource.Id.AddNew:
                    this.StartCamera();
                    return true;
                default:
                    return base.OnOptionsItemSelected(item);
            }
        }

        private Boolean isMounted
        {
            get
            {
                return Android.OS.Environment.ExternalStorageState.Equals(Android.OS.Environment.MediaMounted);
            }
        }

        private void StartCamera()
        {
            var imageUri = ContentResolver.Insert(isMounted ? MediaStore.Images.Media.ExternalContentUri
                                    : MediaStore.Images.Media.InternalContentUri, new ContentValues());

            this.imageUriString = imageUri.ToString();

            var cameraIntent = new Intent(MediaStore.ActionImageCapture);
            cameraIntent.PutExtra(MediaStore.ExtraOutput, imageUri);
            this.StartActivityForResult(cameraIntent, TakePicture);
        }

        private void SelectImageFromStorage()
        {
            Intent intent = new Intent();
            intent.SetType("image/*");
            intent.SetAction(Intent.ActionGetContent);
            this.StartActivityForResult(Intent.CreateChooser(intent,
                    "Select Picture"), SelectPicture);

        }

        // Example code of using the result, in my case i want to upload in another activity
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            // If a picture was taken
            if (resultCode == Result.Ok && requestCode == TakePicture)
            {
                // For some devices data can become null when using the camera activity.
                // For this reason we save pass the already saved imageUriString to the upload activity
                // in order to adapt to every device. Instead we would want to use the data intent
                // like in the SelectPicture option.

                var uploadIntent = new Intent(this.BaseContext, typeof(UploadActivity));
                uploadIntent.PutExtra("ImageUri", this.imageUriString);
                this.StartActivity(uploadIntent);
            }
            // User has selected a image from storage
            else if (requestCode == SelectPicture)
            {
                var uploadIntent = new Intent(this.BaseContext, typeof(UploadActivity));
                uploadIntent.PutExtra("ImageUri", data.DataString);
                this.StartActivity(uploadIntent);
            }
        }
    }
}
Community
  • 1
  • 1
Lucas Arrefelt
  • 3,879
  • 5
  • 41
  • 71