我正在使用cordova相机插件拍照并稍后将其上传到服务器。我一直在手机中使用棒棒糖操作系统,它在我的手机中工作得很好。但是当我在朋友的手机上测试应用程序时,这是在Kitkat操作系统上运行时,会抛出一条错误消息。下面给出了相同的屏幕截图。
给定图像的文件URI类似于 'file:///storage/emulated/0/Android/data/com.app.name/cache/filename.jpg'。我的JavaScript代码如下:
//function that gets called when clicking capture photo button
function capturePhoto() {
navigator.camera.getPicture(onCapturePhoto, onFail, {
quality: 50,
destinationType: Camera.DestinationType.FILE_URI
});
function onCapturePhoto(fileURI) {
var win = function (r) {
clearCache();
retries = 0;
BootstrapDialog.alert('Done uploading image!');
var img = JSON.parse(r.response);
$("#clientid_issue_id").val(img.id.$id);
construct_img_thumbnail_view(img);
$("#go_to_issue_report_btn").prop('disabled', false);
}
var fail = function (error) {
if (retries == 0) {
retries++
setTimeout(function () {
onCapturePhoto(fileURI)
}, 5000)
} else {
retries = 0;
clearCache();
BootstrapDialog.alert('Ups. Something wrong happens!' + JSON.stringify(error));
$("#go_to_issue_report_btn").prop('disabled', false);
}
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
options.mimeType = "image/jpeg";
var params = new Object();
params.client_device_id = device.uuid;
params.client_issue_id = $("#clientid_issue_id").val();
if (params.client_issue_id.trim() === "")
{
params.client_issue_id = 0;
}
options.params = params; // if we need to send parameters to the server request
var ft = new FileTransfer();
$("#go_to_issue_report_btn").prop('disabled', true);
ft.upload(fileURI, encodeURI(SERVER_PATH + "/services/uploadimage"), win, fail, options);
}
我使用的cordova版本是5.1.1。下面给出了AndroidManifest.xml文件。
<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="0.0.1" package="com.example.hello" 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" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application android:hardwareAccelerated="true" android:icon="@drawable/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.Black.NoTitleBar" 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>
</application>
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="22" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>
</manifest>
这是我的config.xml文件
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.example.hello" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<preference name="loglevel" value="DEBUG" />
<allow-intent href="market:*" />
<name>HelloWorld</name>
<description>
A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<feature name="Whitelist">
<param name="android-package" value="org.apache.cordova.whitelist.WhitelistPlugin" />
<param name="onload" value="true" />
</feature>
<feature name="Camera">
<param name="android-package" value="org.apache.cordova.camera.CameraLauncher" />
</feature>
<feature name="Device">
<param name="android-package" value="org.apache.cordova.device.Device" />
</feature>
<preference name="AndroidPersistentFileLocation" value="Internal" />
<preference name="AndroidPersistentFileLocation" value="Compatibility" />
<feature name="File">
<param name="android-package" value="org.apache.cordova.file.FileUtils" />
<param name="onload" value="true" />
</feature>
<feature name="FileTransfer">
<param name="android-package" value="org.apache.cordova.filetransfer.FileTransfer" />
</feature>
</widget>
如果有人能告诉我解决方案,那将非常有帮助。
答案 0 :(得分:0)
实际上我发现了这个问题.SERVER_PATH变量的值是从存储在设备上的外部文本文件 read.txt 中读取的。所以在我的朋友移动设备中,read.txt被编辑为一个名为Kingsoft office的应用程序。路径类似于' http://192.168....../e-gov/web/services/uploadimage '这是正确的服务器路径并保存。应用程序启动并正常工作从文件中读取值。但是用于拍照和上传的代码部分,我用过'ft.upload(fileURI, encodeURI(SERVER_PATH +“/ services / uploadimage”),win,fail,options);'。我使用 alert(encodeURI(SERVER_PATH +“/ services / uploadimage”))来检查服务器路径是否正确。提醒的值是“ http://192.168..../e-gov/web%0D%0A/services/uploadimage '这不是实际的服务器地址。所以我将read.txt文件从我的手机发送给移动设备朋友并再次测试应用程序。即使我使用 encodeURI ,我也会得到正确的服务器地址。希望这可能对其他可能遇到同样情况的人有用。
答案 1 :(得分:-1)
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
参考:{{3}}