3

I'm trying to find a way to access full resolution pictures from MonoDroid, after a long time of attempting to port the Java examples to MonoDroid and looking at how for others seem to have gotten, i currently have the following (Which does not work)

    private const int TAKE_PICTURE = 1;

    protected Uri fileUri;

    private void takePhoto(object sender, EventArgs e)
    {
        Intent intent = new Intent(Android.Provider.MediaStore.ActionImageCapture);

        string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),
                                             "hqxmmc/pictures/ikbeneenplaatje.jpg");

        Java.IO.File xfile = new Java.IO.File(path);

        if (xfile.Exists())
        {
            Android.Util.Log.Warn("FILE", "file exists {0} \n overwriting!", xfile.Name);
            xfile.Delete();
            xfile.CreateNewFile();
        }
        else
        {
            Android.Util.Log.Warn("FILE", "file does not exist {0}, creating", xfile.Name);
            xfile.Mkdirs();
            xfile.CreateNewFile();
        }


        fileUri = Android.Net.Uri.FromFile(xfile);

        Android.Util.Log.WriteLine(LogPriority.Warn, "FILE", fileUri.ToString());

        intent.PutExtra(Android.Provider.MediaStore.ExtraOutput, fileUri);
        StartActivityForResult(intent, TAKE_PICTURE);



    }

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {

        base.OnActivityResult(requestCode, resultCode, data);
        if (requestCode == TAKE_PICTURE)
        {
            Uri imageUri = null;
            // Check if the result includes a thumbnail Bitmap
            if (data != null)
            {
                if (data.HasExtra("data"))
                {
                    var thumbnail = data.GetParcelableArrayExtra("data");
                    Android.Util.Log.WriteLine(LogPriority.Info, "DATA", thumbnail.ToString());
                    // TODO Do something with the thumbnail

                    var outputFileUri = data.GetParcelableArrayExtra("outputFileuri");
                    Android.Util.Log.WriteLine(LogPriority.Info, "DATA", outputFileUri.ToString());
                    // TODO Do something with the full image stored
                    // in outputFileUri
                }
            }
            else
            {
                //todo : reload full image form fileuri
                Android.Util.Log.WriteLine(LogPriority.Info, "PATH", fileUri.ToString());
            }

        }
    }

The 'take a picture' screen is displayed, i can take a picture, but that picture is then saved to the default DCIM folder on the device, ignoring the file name and path i specified for it.

When i save the picture, OnActivityResult is called, but its Intent data parameter contains an empty intent.

how do i get access to the full resolution picture and/or thumbnail of the picture i just shot?

poupou
  • 43,413
  • 6
  • 77
  • 174
Timothy Groote
  • 8,614
  • 26
  • 52
  • _imageUri is null when I run this code. Is the activity garbage collected during the camera intent? – ogborstad Dec 16 '11 at 18:36
  • imageUri should never be garbage collected until the entire activity is dismissed in this case. are you sure you have `BtnCameraClick` hooked up? – Timothy Groote Dec 19 '11 at 23:29

1 Answers1

3

I'll simply posted the code that i have used in my project. Hope it will help you. I have also tried to set file name and path through ContentValues class, like it is described in Android wiki, but all was ineffectively, I think that it's some MonoDroid bug.

private string _imageUri;

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

public void BtnCameraClick(object sender, EventArgs eventArgs)
{
      var uri = ContentResolver.Insert(isMounted ? MediaStore.Images.Media.ExternalContentUri
                              : MediaStore.Images.Media.InternalContentUri, new ContentValues());
     _imageUri = uri.ToString();
      var i = new Intent(MediaStore.ActionImageCapture);
      i.PutExtra(MediaStore.ExtraOutput, uri);
      StartActivityForResult(i, CAPTURE_PHOTO);
}

    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        if (resultCode == Result.Ok && requestCode == CAPTURE_PHOTO)
        {
          Toast.MakeText(this, string.Format("Image URI is {0}",_imageUri), ToastLength.Short).Show();    
        }
    }
Timothy Groote
  • 8,614
  • 26
  • 52
mironych
  • 2,938
  • 2
  • 28
  • 37