相机插件不适用于 Android 的 PhoneGap Build
Posted
技术标签:
【中文标题】相机插件不适用于 Android 的 PhoneGap Build【英文标题】:Camera Plugin is not working on PhoneGap Build for Android 【发布时间】:2018-04-28 09:33:50 【问题描述】:我正在创建一个 PhoneGap 应用。我安装了带有“phonegap plugin add cordova-plugin-camera”的相机应用程序。它在 phonegap 应用程序中运行良好,但是在我成功构建并将其上传到 phonegap 构建之后,这些按钮都没有在我的 android 手机上工作。我在下面发布我的索引、配置和清单。感谢您的帮助。
索引.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
<link rel="stylesheet" href="jq/jquery.mobile-1.4.5.css" />
<script type="text/javascript" src="cordova.js"></script>
<script src="jq/jquery-3.2.1.js"></script>
<script src="jq/jquery.mobile-1.4.5.js"></script>
<title>Blank App</title>
<script type="text/javascript" charset="utf-8">
var pictureSource;
var destinationType;
document.addEventListener("deviceready",onDeviceReady,false);
function onDeviceReady()
pictureSource=navigator.camera.PictureSourceType;
destinationType=navigator.camera.DestinationType;
function onPhotoDataSuccess(imageData)
var smallImage = document.getElementById('smallImage');
smallImage.style.display = 'block';
smallImage.src = "data:image/jpeg;base64," + imageData;
function onPhotoURISuccess(imageURI)
alert("inside large image")
var largeImage = document.getElementById('largeImage');
largeImage.style.display = 'block';
largeImage.src = imageURI;
function capturePhoto()
navigator.camera.getPicture(onPhotoDataSuccess, onFail, quality: 50,
destinationType: destinationType.DATA_URL );
function capturePhotoEdit()
navigator.camera.getPicture(onPhotoDataSuccess, onFail,
quality: 20, allowEdit: true,
destinationType: destinationType.DATA_URL );
function getPhoto(source)
navigator.camera.getPicture(onPhotoURISuccess, onFail, quality: 50,
destinationType: destinationType.FILE_URI,
sourceType: source );
function onFail(message)
alert('Failed because: ' + message);
</script>
</head>
<body>
<button onclick="capturePhoto();">Capture Photo</button> <br>
<button onclick="capturePhotoEdit();">Capture Editable Photo</button> <br>
<button onclick="getPhoto(pictureSource.PHOTOLIBRARY);">From Photo Library</button><br>
<button onclick="getPhoto(pictureSource.SAVEDPHOTOALBUM);">From Photo Album</button><br>
<img style="display:none;width:60px;height:60px;" id="smallImage" src="" />
<img style="display:none;" id="largeImage" src="" />
</body>
</html>
配置.xml
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.phonegap.helloworld" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
<name>App005</name>
<description>
A blank PhoneGap app.
</description>
<author email="support@phonegap.com" href="http://phonegap.com">
PhoneGap Team
</author>
<content src="index.html" />
<access origin="*" />
<engine name="android" spec="~6.3.0" />
<plugin name="cordova-plugin-camera" spec="~3.0.0" />
</widget>
AndroidManifest.xml
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="10000" android:versionName="1.0.0" package="com.phonegap.helloworld" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true">
<activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/activity_name" android:launchMode="singleTop" android:name="MainActivity" android:theme="@android:style/Theme.DeviceDefault.NoActionBar" android:windowSoftInputMode="adjustResize">
<intent-filter android:label="@string/launcher_name">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider android:authorities="$applicationId.provider" android:exported="false" android:grantUriPermissions="true" android:name="android.support.v4.content.FileProvider">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
</provider>
</application>
<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="26" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
【问题讨论】:
【参考方案1】:我使用的是更新版本的插件。我将它们更改为旧版本,现在一切正常。
<plugin name="cordova-plugin-camera" spec="2.4.1" />
<plugin name="cordova-plugin-compat" spec="1.2.0" />
<plugin name="cordova-plugin-whitelist" spec="1.3.3" />
<plugin name="cordova-plugin-file" spec="4.3.1" />
<plugin name="cordova-plugin-file-transfer" spec="1.6.1" />
<plugin name="cordova-plugin-media-capture" spec="1.4.3" />
【讨论】:
【参考方案2】:我在 Android 上使用 Phonegap 版本 7.0.1 和 cordova-plugin-camera 时遇到了类似的问题。它在 ios 上按预期工作。将插件更新到旧版本已解决此问题。谢谢
【讨论】:
你也改了哪个版本? 我改成cli-6.5.0以上是关于相机插件不适用于 Android 的 PhoneGap Build的主要内容,如果未能解决你的问题,请参考以下文章
来自画廊工作的文件选择器,但它不适用于 android webview 中的相机