简体   繁体   中英

Issue with cordova camera plugin on Kitkat OS

I am using cordova camera plugin to take picture and later upload it to the server.I have been using lollipop OS in my phone and it works pretty well in my phone.However when I test the app on my friend's phone which is running on Kitkat operating system, an error message is thrown.The screenshot for the same is given below.

在此处输入图片说明

The file URI for the given image is something like 'file:///storage/emulated/0/Android/data/com.app.name/cache/filename.jpg'.My JavaScript code is given below

 //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);
                }

The version of cordova I am using is 5.1.1.The AndroidManifest.xml file is given below.

<?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>

Here is my config.xml file

<?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>

It would be very helpful if someone could tell me the solution.

Actually I found out the issue.The value of SERVER_PATH variable is read from an external text file read.txt stored on the device.So in my friends mobile,the read.txt is edited with an App called Kingsoft office.The path is given like ' http://192.168....../e-gov/web/services/uploadimage ' which is the correct server path and saved.The application starts and works fine reading the value from the file.But in the code section for taking picture and upload, I used like 'ft.upload(fileURI, encodeURI(SERVER_PATH + "/services/uploadimage") , win, fail, options);'. I used alert(encodeURI(SERVER_PATH + "/services/uploadimage")) to check whether the server path is right.Surprizingly the value alerted was ' http://192.168..../e-gov/web%0D%0A/services/uploadimage ' which was not the actual server address.So I send the read.txt file from my phone to friends mobile and tested the app again.Now even if I do use encodeURI I get the correct server address.Hope this might be useful to some other people who may ran into same sort of situation.

YU No permit camera?

 <uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />

Reference: http://developer.android.com/reference/android/hardware/Camera.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM