Ionic应用程序-访问照片库的Android问题

taor4pac  于 2023-03-16  发布在  Ionic
关注(0)|答案(1)|浏览(161)

我正在创建一些离子电容器应用程序,我有新的Android手机的问题。我有一个屏幕上的一些按钮,允许用户设置他的个人资料图片。当用户第一次点击按钮时,应用程序要求权限“允许{APP_NAME}访问此设备上的照片,视频,音乐和音频?”当用户允许此权限时,仍然无法打开图库选择照片。
我可以在控制台中看到此错误消息:
I/Capacitor/Console: File: http://localhost/default-src_app_shared_shared_module_ts.js - Line 2195 - Msg: Error: Missing the following permissions in AndroidManifest.xml: android.permission.WRITE_EXTERNAL_STORAGE
我确信我在AndroidManifest.xml中有此权限,因为我检查了几次,它就在那里。我找不到任何可以帮助我修复它的东西。
我的机器人清单.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="io.the.app">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <!-- capacitor-background-geolocation licence -->
        <meta-data android:name="com.transistorsoft.locationmanager.license" android:value="" />
        <activity
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|smallestScreenSize|screenLayout|uiMode"
            android:name="io.the.app.MainActivity"
            android:label="@string/title_activity_main"
            android:theme="@style/AppTheme.NoActionBarLaunch"
            android:launchMode="singleTask"
            android:exported="true">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.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>

    <!-- Permissions -->

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.location.gps" />
</manifest>

我的电容器医生:

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 4.7.0
  @capacitor/core: 4.7.0
  @capacitor/android: 4.7.0
  @capacitor/ios: 4.7.0

Installed Dependencies:

  @capacitor/cli: 4.6.1
  @capacitor/core: 4.6.1
  @capacitor/android: 4.6.1
  @capacitor/ios: 4.6.1

[success] iOS looking great! 👌
[success] Android looking great! 👌

我的相机插件版本:
"@capacitor/camera": "^4.1.4",
我的添加照片的函数看起来像这样:

handleAddPhoto = async () => {
    await Camera.checkPermissions().then((permissions) => {
        if (permissions.photos !== 'granted') {
            Camera.requestPermissions();
        }
    });
    const photo = await Camera.getPhoto({
        quality: 50,
        resultType: CameraResultType.Base64,
        source: CameraSource.Photos,
    });
    this.avatarSrc = this.sanitizeSrc(photo.base64String);
    this.cdr.detectChanges();
    this.handleSavePhoto('data:image/jpeg;base64,' + photo.base64String);
};
uxh89sit

uxh89sit1#

这似乎是一个异步处理的问题。我有点惊讶,这为您正确编译。
.then附加到checkPermissions调用可能会有效地否定await
如果是这样,您的应用程序将在请求权限之前继续沿着getPhoto调用。

async function getCameraPermission(): Promise<boolean> {
  if (hasCameraPermission(await Camera.checkPermissions())) {
    return true;
  }

  const result = hasCameraPermission(await Camera.requestPermissions({ permissions: ['camera'] }));

  // We need to wait for a moment to let the UI catch up on iOS so that the user doesn't have to request the camera more than once. This
  // will only occur where camera permissions have not been requested or revoked. Once permissions have been granted, this code will be
  // unreachable.
  await sleep(500);

  return result;
}

function hasCameraPermission({ camera }: PermissionStatus): boolean {
  switch (camera) {
    case 'granted':
      return true;
  }
  return false;
}

async function handleAddPhoto(): Promise<string> {
  if (await getCameraPermission()) {
    const { dataUrl } = await Camera.getPhoto({
        quality: 50,
        resultType: CameraResultType.Base64,
        source: CameraSource.Photos,
    });

    if (dataUrl) {
      return dataUrl;
    }
  }

  throw new Error('Permission to access the camera was denied or the camera returned nothing.');
}

相关问题