使用MonoDroid从相机访问全分辨率图片

时间:2011-11-09 16:30:27

标签: c# android .net xamarin.android android-camera

我试图找到一种方法来访问MonoDroid的全分辨率图片,经过长时间尝试将Java示例移植到MonoDroid并查看其他人似乎已经得到了如何,我目前有以下(其中不起作用)

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

        }
    }

显示“拍照”屏幕,我可以拍照,但该照片会保存到设备上的默认DCIM文件夹中,忽略我为其指定的文件名和路径。

当我保存图片时,会调用OnActivityResult,但其Intent data参数包含空意图。

如何访问刚刚拍摄的全分辨率图片和/或缩略图?

1 个答案:

答案 0 :(得分:3)

我只是发布了我在项目中使用的代码。希望它会对你有所帮助。我也尝试通过ContentValues类设置文件名和路径,就像在Android wiki中描述的那样,但都是无效的,我认为这是一些MonoDroid错误。

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