Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

c# - access to full resolution pictures from camera with MonoDroid

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} 
 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();    
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...