Monodroid Сделать снимок с помощью камеры

Я пробовал это сам, но безуспешно, я могу сделать это на Java, но на С# это немного отличается. Любая помощь будет отличной. Все, что я хочу, это:

  1. Запустите камеру.
  2. Сфотографировать.
  3. Просмотрите фотографию в режиме просмотра изображения.

person Phil Kearney    schedule 08.12.2011    source источник
comment
Я ответил на тот же вопрос здесь изображения в полном разрешении с камеры с монодроидом"> stackoverflow.com/questions/8068156/   -  person mironych    schedule 09.12.2011


Ответы (1)


Я знаю, что это довольно старый вопрос, но я отвечу на него своим кодом, который я использовал. По моему опыту работы с этим, не все устройства могут использовать стандартное намерение и возвращать данные обратно в файл OnActivityResult.

private void TakePicture()
    {          
        if (PackageManager.HasSystemFeature(PackageManager.FeatureCamera))
        {
            var intent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
            var file = new File(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures), "PictureTHIS");
            if (!file.Exists())
            {
                if (!file.Mkdirs())
                {
                    Log.Debug(Constants.LOG_TAG, "Unable to create directory to save photos.");
                    return;
                }
            }
            _file = new File(file.Path + File.Separator + "IMG_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".jpg");
            intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(_file));
            StartActivityForResult(intent, Constants.CAMERA_ACTIVITY);
        }
        else
        {
            Toast.MakeText(this, "This device does not have a camera, please select an image from another option.", ToastLength.Short).Show();
        }
    }

Я использовал это в качестве справки о том, как поступать с некоторыми устройствами, которые не возвращают намерение. http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/

      protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        if (resultCode == Result.Ok)
        {
    //checking for camera acitivy, my app allows both gallery and camera for retrieving pictures.
            if (requestCode == Constants.CAMERA_ACTIVITY)
            {                  
        //some devices do not pass back an intent so your data could be null
                if (data != null && !string.IsNullOrEmpty(data.DataString))
                {
        //full uri to where the image is on the device.
        Android.Net.Uri.Parse(data.DataString);
                }
                else
                {
                    //issue where some devices don't pass back correct data from the intent.
                    var uris = GetImagePathFromCamera();
                    if (uris == null)
                    {
            //had an issue with some devices with no sd card, so i create the file and store it on the device
                        var orientation = 0;
                        var date = string.Empty;
            //_file is a java.io.file that is passed in the intent with get extra specified.
                        try
                        {
                            var exif = new ExifInterface(_file.Path);
                            orientation = exif.GetAttributeInt(ExifInterface.TagOrientation, -1);
                            date = exif.GetAttribute(ExifInterface.TagDatetime);
                        }
                        catch { }

                        switch (orientation)
                        {
                            case 6:
                                Helpers.Orientation = 90;
                                break;
                            case 3:
                                Helpers.Orientation = 180;
                                break;
                            case 8:
                                Helpers.Orientation = 270;
                                break;
                            default:
                                Helpers.Orientation = 0;
                                break;
                        }

                        ContentValues values = new ContentValues();
                        values.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.DisplayName, _file.Name);
                        values.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.DateTaken, date);
                        values.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.MimeType, "image/jpeg");
                        values.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Orientation, Helpers.Orientation);
                        values.Put(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data, _file.Path);

                        var uri = ContentResolver.Insert(Android.Provider.MediaStore.Images.Media.ExternalContentUri, values);
                        //uri returned is the path to the image on the device
                    }
                    else
                    {
                        //uris is a list of uri's specifying the image taken and its thumbnail
                    }
                };
            }
        }
        else
    {
    //notify user the camera was cancelled if you want
    }
}
person Bryan    schedule 03.08.2012