nstudio / nativescript-camera-plus

MIT License
79 stars 50 forks source link

Cannot open the device gallery on Android #172

Open dpdragnev opened 8 months ago

dpdragnev commented 8 months ago

Hello, here is my setup:

...
<!-- Devices running Android 12L (API level 32) or lower  -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="32" tools:replace="android:maxSdkVersion" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- Devices running Android 13 (API level 33) or higher -->
    <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
    <!-- To handle the reselection within the app on Android 14 (API level 34) -->
    <uses-permission android:name="android.permission.READ_MEDIA_VISUAL_USER_SELECTED" />
    <uses-permission android:name="android.permission.CAMERA" />
...

When the user opens the camera, a popup asking for permission to access the camera appears, and after the user accepts, they can use the camera. However, when they try to access the gallery (the popup to allow access shows up and it is accepted), I see the following message in the console:

NativeScript-CameraPlus --- [disposeNativeView.]

At this point, the camera closes and nothing happens.

Here is my code:

export function pickImageFromGallery() {
    if (!cam)  cam = new CameraPlus();

    cam.galleryPickerMode = 'single';

    if (isAndroid) {
        if (!cam.hasStoragePermissions()) {
            cam.requestStoragePermissions().then((response: boolean) => {
                if (response) {
                    doPickFromGallery();
                }
            });
        } else {
            doPickFromGallery();
        }
    } else {
        doPickFromGallery(); // iOS
    }
}

function doPickFromGallery() {
    cam.chooseFromLibrary().then((imageAssets: any) => {
       console.log('image passed back: ', imageAssets);
       ... rest of the code
    });
}

This happens on a simulator (Pixel 7, API 33) as well as on a physical device (Samsung Note 9, Android 10, API 30).

Any help in resolving this would be greatly appreciated.

dpdragnev commented 8 months ago

I spent a few days trying to fix this issue without success, so I switched to https://github.com/NativeScript/plugins/tree/main/packages/imagepicker#nativescriptimagepicker. I just replaced the chooseFromLibrary() method with the corresponding method from the ImagePicker plugin.