1Answer
  • 15
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

TakePhotoAsync Camera not open

I follow very tutorials and answers here in site without success. Spending lot time.

Using Media.Plugin, method TakePhotoAsync() not open the camera (Genymotion 9.0 API-28 | device samsung j7 prime. android 8.0).

PickPhotoAsync() working wonderful.

AndroidManifest.xml

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

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

file_path.xml

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

AssemblyInfo.cs

[assembly: UsesPermission(Android.Manifest.Permission.Internet)]
[assembly: UsesPermission(Android.Manifest.Permission.WriteExternalStorage)]
[assembly: UsesFeature("android.hardware.camera", Required = false)]
[assembly: UsesFeature("android.hardware.camera.autofocus", Required = false)]

MainActivity.cs

protected override async void OnCreate(Bundle savedInstanceState)
{
    try
    {
...
        base.OnCreate(savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
        await CrossMedia.Current.Initialize();
        CrossCurrentActivity.Current.Init(this, savedInstanceState);
        UserDialogs.Init(this);
        LoadApplication(new App());
    }
    catch (Exception ex)
    {
        Console.WriteLine("EXCEPTION HERE => "+ex);

...

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

METHOD.XAML.CS

Problem in >>var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions.. But not erros, exception, etc...

async void TakePhoto(object sender, EventArgs args)
        {
            try
            {
                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    await DisplayAlert("Ops", ":( Nenhuma câmera detectada.", "OK");
                    return;
            }

            var cameraStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
            var storageStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);

            if (cameraStatus != PermissionStatus.Granted || storageStatus != PermissionStatus.Granted)
            {
                var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Camera, Permission.Storage });
                cameraStatus = results[Permission.Camera];
                storageStatus = results[Permission.Storage];
            }

            if (cameraStatus == PermissionStatus.Granted && storageStatus == PermissionStatus.Granted)
            {

        //===HERE IS THE PROBLEM, READ METHOD BUT NOT OPEN CAMERA! NOT ERRORS, NOT EXCEPTION, NOTHING===
                var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
                    SaveToAlbum = true,
                    //Directory = "Sample",
                    //Name = "test.jpg"
                });


                if (file == null)
                    return;

                await DisplayAlert("File Location", file.Path, "OK");

                if (MyImage.Source == null)
                {
                    Console.WriteLine("MyImage.Source == null ==> OK");
                }

                MyImage.Source = ImageSource.FromStream(() =>
                {
                    var stream = file.GetStream();
                    file.Dispose();
                    return stream;
                });
            }
            else
            {
                await DisplayAlert("Permissions Denied", "Unable to take photos.", "OK");
            }

        }
        catch (Exception e)
        {
            Console.WriteLine("EXCEPTION HERE =: "+e);
        }
    }

I test your code, this error was by the file_path.xml

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

If I change Path of my_images to <external-files-path name="my_images" path="Pictures" /> ,you can take a Photo as normal

Here is my demo.

https://github.com/851265601/TakePhotoDemo

Here is GIF of my demo.

enter image description here

  • 1
Reply Report
      • 1
    • If the reply is helpful, please do not forget to mark it as an answer( please click the “?” at the right of answer). It will help others who have similar issue.
    • I thank you for having tested my code. But I am getting the following exception: EXCEPTION HERE =: System.ArgumentException: Unable to get file location. This most likely means that the file provider information is not set in your Android Manifest file. Because this I was using that way: forums.xamarin.com/discussion/119942/…