1
A
回答
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
}
}
相关问题
- 1. 从相机拍摄照片
- 2. 使用UIViewController(MonoTouch)相机拍摄照片
- 3. 相机照片拍摄失败
- 4. IntentFilter相机和拍摄的照片
- 5. 的Android相机拍摄多张照片
- 6. 拍摄来自相机的照片
- 7. 使用CvVideoCamera拍摄照片
- 8. 使用QTCaptureView拍摄照片
- 9. Phonegap相机API保存拍摄到相机胶卷的照片
- 10. 使用Android相机拍摄照片后开始新活动
- 11. 阅读使用相机拍摄的照片意向
- 12. 使用集成相机自动拍摄照片
- 13. 使用iPhone 7 Plus相机同时拍摄照片
- 14. 使用Android相机拍摄的照片的奇怪尺寸
- 15. 使用相机拍摄照片后堆内存越来越多
- 16. 使用Photos Framework获取相机拍摄的照片的URL
- 17. 使用相机拍摄照片时的缩放选项
- 18. 如何使用相机拍摄的照片?
- 19. 使用相机拍摄照片后,意图为空
- 20. 如何在windows phone/desktop上使用相机拍摄照片/照片
- 21. 使用phonegap Camera Plugin从照相机卷中随机拍摄照片?
- 22. 拍摄照片后无法选择使用照片或重拍
- 23. 如何检测照片是否被拍摄(照相机意图)
- 24. 如何从照相机拍摄的照片制作缩略图?
- 25. 从Galley和照相机拍摄照片在Tamarin.forms
- 26. 需要用MonoDroid和MVVMCross拍摄照片的一个例子
- 27. 在iOS中使用数码相机拍摄相机中的照片
- 28. 用相机拍摄照片并用GWT上传器发送
- 29. 相机拍摄照片在Android临时保存图片?
- 30. 拍摄照片并保存在SD卡或相机相册PHONEGAP
我已经回答了这里同样的问题http://stackoverflow.com/questions/8068156/access-to-full-resolution-pictures-from-camera-with-monodroid/8072724#8072724 – mironych