我知道这个问题并不新鲜,但是我确实坚持了好几天,我需要您的支持。 我的问题是: 我无法使用Android的插件媒体在我的棱镜xamarin表单项目中拍照(尽管它可以从图库中选择图片)。 我得到的错误是:
无法获取XAM插件媒体站点的文件位置
我希望收到您的来信,并在此先感谢您的支持。
这是我的代码:
Android清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.niternational.Hope" android:installLocation="auto">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<application android:label="Hope.Android" android:icon="@mipmap/ic_launcher">
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.niternational.Hope.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
</manifest>
资源xml文件夹上的file_paths文件
<?xml version="1.0" encoding="utf-8" ?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Pictures" />
<external-files-path name="my_movies" path="Movies" />
</paths>
主要活动
[Activity(Label = "Hope", Icon = "@mipmap/ic_launcher", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
CrossCurrentActivity.Current.Init(this, bundle);
LoadApplication(new App(new AndroidInitializer()));
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)
{
Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
public class AndroidInitializer : IPlatformInitializer
{
public void RegisterTypes(IContainerRegistry container)
{
// Register any platform specific implementations
}
}
AssemblyInfo文件 我添加了:
[assembly: UsesFeature("android.hardware.camera", Required = false)]
[assembly: UsesFeature("android.hardware.camera.autofocus", Required = false)]
隐藏代码
private async void TakeImage()
{
var actions = await Application.Current.MainPage.DisplayActionSheet("ActionSheet: Image?", "Cancel", null, "Take Picture", "From Gallery");
switch (actions)
{
case "Take Picture":
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsPickPhotoSupported)
{
var message = "No Camera available";
//DependencyService.Get<IMessage>().ShortAlert(message);
return;
}
var mediaOptions = new StoreCameraMediaOptions
{
Directory = "UploaDoc",
Name = $"{DateTime.UtcNow}.jpg",
SaveToAlbum = true,
CompressionQuality = 75,
CustomPhotoSize = 50,
PhotoSize = PhotoSize.MaxWidthHeight,
MaxWidthHeight = 2000,
DefaultCamera = CameraDevice.Front
};
// Take a photo of the business receipt.
var file = await CrossMedia.Current.TakePhotoAsync(mediaOptions);
if (file == null)
return;
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
return stream;
});
var memoryStream = new MemoryStream();
file.GetStream().CopyTo(memoryStream);
file.Dispose();
imgAsBytes = memoryStream.ToArray();
memoryStream.Dispose();
break;
case "From Gallery":
if (!CrossMedia.Current.IsPickPhotoSupported)
{
var message = "Picking image is not supported";
//DependencyService.Get<IMessage>().ShortAlert(message);
return;
}
var files = await CrossMedia.Current.PickPhotoAsync();
if (files == null)
return;
image.Source = ImageSource.FromStream(() =>
{
var stream = files.GetStream();
return stream;
});
var ms = new MemoryStream();
files.GetStream().CopyTo(ms);
files.Dispose();
imgAsBytes = ms.ToArray();
ms.Dispose();
break;
}