我正在使用Xamarin.Mobile,我可以启动并拍照。虽然看起来无法保存图片。这是Xamarin在使用Xamarin.Mobile组件时提供的代码。
如何在Android中保存MediaFile文件?
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
{
var m_View = inflater.Inflate (Resource.Layout.Feed, null);
btnCamera = m_View.FindViewById<Button> (Resource.Id.btnCamera);
btnGallery = m_View.FindViewById<Button> (Resource.Id.btnGallery);
btnCamera.Click += (sender, e) => {
//launch gallery
//allow photo editing/saving
var mediaPicker = new MediaPicker (Activity);
if (!mediaPicker.IsCameraAvailable){
//Console.WriteLine ("No Camera!");
}else {
var intent = mediaPicker.GetTakePhotoUI (new StoreCameraMediaOptions {
Name = "test.jpg",
Directory = "APPNAME"
});
StartActivityForResult(intent, 1);
}
};
btnGallery.Click += (sender, e) => {
//launch gallery
//allow photo editing/saving
};
return m_View;
}
protected virtual async void OnActivityResult (int requestCode, Result resultCode, Intent data)
{
//user canceled
if (resultCode == Result.Canceled)
return;
MediaFile file = await data.GetMediaFileExtraAsync (Activity);
file.Path
//System.Console.WriteLine (file.Path);
}
答案 0 :(得分:0)
确保您已在AndroidManifest.xml文件中添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />