dimonovdd / Xamarin.MediaGallery

This plugin is designed to picking and save images and video files from native gallery of Android and iOS devices and capture photos
MIT License
148 stars 18 forks source link

Camera app doesn't open on CapturePhotoAsync #88

Closed smalgin closed 2 years ago

smalgin commented 2 years ago

Description

Camera app doesn't open on CapturePhotoAsync

Expected behavior

Should open the camera app. Xamarin.Essentials media picker works.

Code snippet

if (MediaGallery.CheckCapturePhotoSupport()) return;

        var status = await Permissions.RequestAsync<Permissions.Camera>();

        if (status != PermissionStatus.Granted)
            return;

        using var photoFile = await MediaGallery.CapturePhotoAsync();

Configuration

dimonovdd commented 2 years ago

1) What value does CheckCapturePhotoSuppor method return? 2) Can you show your android manifest file? 3) Can you provide more information? LogCat or behavior on other devices on your app

joeykang commented 2 years ago

I had the same issue on Android. Looks like it happens when the app was built with the target of API level 30 in AndroidManifest.xml. It was ok with API level 29.

CheckCapturePhotoSupport returns false in this issue. If I ignore CheckCapturePhotoSupport() and proceed the app will crash.

dimonovdd commented 2 years ago

@joeykang hi. Can you send me your AndroidManifest.xml?

joeykang commented 2 years ago
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" package="com.myapp.mobile" android:installLocation="auto" android:versionName="2.1.2">
    <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="30" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <application android:label="MyApp">
        <provider android:name="android.support.v4.content.FileProvider" android:authorities="com.myapp.mobile.fileprovider" android:exported="false" android:grantUriPermissions="true">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data>
        </provider>
        <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="myapp_general" />
        <service android:name="com.myapp.mobile.LocalFirebaseMessagingService" android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    </application>
</manifest>
dimonovdd commented 2 years ago

@joeykang You should configure your AndroidManifest.xml as described in Readme

joeykang commented 2 years ago

@joeykang You should configure your AndroidManifest.xml as described in Readme

Comparing my AndroidManifest.xml with the one in README, a setting below was missing in my AndroidManifest.xml

<uses-feature android:name="android.hardware.camera" android:required="true" />

I added it in my AndroidManifest.xml but no luck with that on Google Pixel 3a and Android 30 emulator.

dimonovdd commented 2 years ago

@joeykang it is also mandatory

<queries>
  <intent>
    <action android:name="android.media.action.IMAGE_CAPTURE" />
  </intent>
</queries>
joeykang commented 2 years ago

@dimonovdd it worked ok with what you mentioned. There was my misunderstanding of configurations for Android. I appreciate your quick response.

@joeykang it is also mandatory

<queries>
  <intent>
    <action android:name="android.media.action.IMAGE_CAPTURE" />
  </intent>
</queries>
dimonovdd commented 2 years ago

@smalgin Have you tried it? Can I close the issue? @joeykang I'm glad. Next time you should be more attentive to a little things😉

smalgin commented 2 years ago

I'll try in a couple days. Too busy with the day's work.

smalgin commented 2 years ago

In emulator: Did everything as prescribed, still Google Drive won't let me pick any files. Tried to cap from Emulated camera - img is captured but no mediafile at the exit. Then my Emulator froze. Resetting Emulator to factory defaults...

smalgin commented 2 years ago

Ok, so look like it doesn't return from the pick/cap activity properly at all - bp placed after PickAsunc/CaptureAsync doesn't hit. I tried with the latest (2.0.0) the rest is the same as in bug description. I'll stick to X.Essentials for now & maybe try your plugin later when migrate to MAUI. Good luck and thanks!

dimonovdd commented 2 years ago

Can you send me a small sample project? I would like to fix the bugs if there is one. But I can't reproduce it.

dimonovdd commented 2 years ago

Currently 2.1.1 is the latest version of the plugin

smalgin commented 2 years ago

I suspect that if I create empty Xamarin project w latest everything it might just work. I'll try, but ETA is unclear, sorry - too much stuff is happening around.

Lukata commented 2 years ago

Hello @smalgin

I faced the same issue on iOS and the problem was that I wasn't asking for the permission to open the camera.

Something like that solve my issue:

`var status = await Permissions.CheckStatusAsync();

if(status != PermissionStatus.Granted) { var request = await Permissions.RequestAsync(); if (request == PermissionStatus.Denied) return null; } if (DeviceInfo.Platform == DevicePlatform.iOS) { IMediaFile media = await MediaGallery.CapturePhotoAsync(); return media; }`

dimonovdd commented 2 years ago

@Lukata Hi. Can you provide a sample?

I think you should change the function of getting a controller:

https://github.com/dimonovdd/Xamarin.MediaGallery#ios-optional

Lukata commented 2 years ago

Hi @dimonovdd,

I solved my issue with the code I sent in my previous message :)

I just answered to this topic because it might be related because on Android too I had this issue and the problem was that I was missing the permission request (MediaGallery doesn’t open if you don’t ask the permission, and by default the permission is not Denied but Unknown).

best regards,

Lucas.