相机未在xamarin中打开

时间:2018-04-27 08:05:54

标签: android xamarin xamarin.android

我是xamarin的初学者。我在xamarin中有Android和ios的现有项目。我已经在ios中成功打开了摄像头但在android中没有打开。以下是我的代码。

static async Task<Plugin.Media.Abstractions.MediaFile> TakePicture()
        {
            await CrossMedia.Current.Initialize();

            if (CrossMedia.Current.IsTakePhotoSupported)
            {
                //var imageProcessing = DependencyService.Get<IImageProcessing> ();
                //              imageProcessing.TakePhoto ();
                //              return null;

                var options = new Plugin.Media.Abstractions.StoreCameraMediaOptions();

                //options.Directory = imageProcessing.GetImageDir ();
                return await CrossMedia.Current.TakePhotoAsync(options);
            }
            else
            {
                return null;
            }
        }

我已添加 manifest.xml 的权限。当我在我的设备中运行项目时,它会请求权限但不会打开相机。

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

要使用Media Plugin for Xamarin,您需要执行readme.txt中所述的其他必要设置。

其他必需设置(请阅读!)

的Android

在您的BaseActivity或MainActivity(对于Xamarin.Forms)中添加以下代码:

添加使用:

using Plugin.Permissions;
using Plugin.Permissions.Abstractions;

然后添加到Activity:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
    PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}

需要WRITE_EXTERNAL_STORAGEREAD_EXTERNAL_STORAGE权限,但图书馆会自动为您添加此权限。此外,如果您的用户正在运行Marshmallow,插件将自动提示他们获取运行时权限。

ANDROID N 如果您的应用程序针对Android N(API 24)或更新版本,则必须使用版本2.6.0 +。

您还必须添加一些其他配置文件以遵守新的严格模式:

1。)将以下内容添加到标记内的AndroidManifest.xml中:

<provider android:name="android.support.v4.content.FileProvider" 
                android:authorities="YOUR_APP_PACKAGE_NAME.fileprovider" 
                android:exported="false" 
                android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" 
                android:resource="@xml/file_paths"></meta-data>
</provider>
必须将

YOUR_APP_PACKAGE_NAME设置为您的应用包名称!

2。)将一个名为xml的新文件夹添加到Resources文件夹中,并添加一个名为file_paths.xml的新XML文件

添加以下代码:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="my_images" path="Pictures" />
    <external-files-path name="my_movies" path="Movies" />
</paths>

YOUR_APP_PACKAGE_NAME必须设置为您的应用套餐名称!

您可以在以下网址了解详情:https://developer.android.com/training/camera/photobasics.html

Android当前活动设置

此插件使用Current Activity Plugin来访问当前的Android Activity。如果MainApplication.cs文件未自动添加到您的应用程序,请务必完成完整设置。请完整阅读Current Activity Plugin Documentation。绝对最低限度,您必须在Activity的OnCreate方法中设置以下内容:

csharp Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;

强烈建议您使用Current Activity Plugin Documentation

中列出的自定义应用程序

答案 1 :(得分:0)

使用此意图

Intent intent = new Intent(MediaStore.ActionImageCapture);                 BitmapHelper._file =新文件(BitmapHelper。 dir,String.Format(“ myProfileTemp {0} .jpg”,Guid.NewGuid()));

            Android.Net.Uri uri = FileProvider.GetUriForFile(this.BaseContext, "com.AppName.app.fileprovider", BitmapHelper._file);
            intent.PutExtra(MediaStore.ExtraOutput, uri);
            StartActivityForResult(intent, CAMERA_REQUEST);

答案 2 :(得分:-1)

添加AndroidManifest.xml

<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.AppName.app.fileprovider" android:exported="false" android:grantUriPermissions="true">
        <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"></meta-data>
    </provider>