I am using phonegap 2.9 for the developement of my android app. I get error code 3 while uploading the image from my phone's galery.
I have been following the documentation of the phonegap provided in http://docs.phonegap.com/en/2.9.0/cordova_camera_camera.md.html#Camera and for uploading the picture i have been following the file transfer phonegap documentation http://docs.phonegap.com/en/2.9.0/cordova_file_file.md.html#File .
I have no problem while selecting the image from the phone's gallery. But I get error in alert box when I upload it to the web server.
The image selection and upload from the phone's gallery to the server works completely fine in samsung grand,samsung s, htc but gives error code 3 i.e connection error while testing in samsung s3 and s4.
Most often, after capturing the image or selecting the image from the gallery the application restarts.
Any kind of help would be appreciated.
the js code:
<script type="text/javascript" charset="utf-8"> window.picsrc=""; window.myImage=""; var pictureSource; // picture source var destinationType; // sets the format of returned value 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; picsrc="data:image/jpeg;base64," + imageData; myImage=imageData; document.getElementById('eme').value=myImage; } function onPhotoURISuccess(imageURI) { var smallImages = document.getElementById('smallImage'); smallImages.style.display = 'block'; smallImages.src = imageURI; <?php if($plat=="iOS"){?> document.getElementById('smallImage').style.display = 'none'; document.getElementById('nophoto').innerHTML = "Your Photo has been recieved"; <?php }?> picsrc=imageURI; myImage=imageURI; } function capturePhoto(){ document.getElementById('upload_uri').style.display= 'none'; document.getElementById('hide_upload').style.display= 'block'; navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50, destinationType: destinationType.DATA_URL, targetWidth: 464}); camera.correctOrientation(true); }); } function capturePhotoEdit() { alert('edit'); 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, correctOrientation: true, allowEdit: true}); document.getElementById('hide_upload').style.display= 'none'; document.getElementById('upload_uri').style.display= 'block'; } function onFail(message) { alert('Failed because: ' + message); } function photoUpload() { var img = document.getElementById('smallImage'); myImage = img.src; if (myImage=="") { alert("take picture first"); } var options = new FileUploadOptions(); options.fileKey="file"; options.fileName=myImage.substr(myImage.lastIndexOf('/')+1); options.mimeType="image/jpeg"; var params = new Object(); params.value1 = "test"; params.value2 = "param"; options.chunkedMode = false; options.params = params; var ft = new FileTransfer(); ft.upload(myImage, "http://app.appyagents.com.au/upload2.php", win, fail, options); } function win(r) { alert("success of the upload"); console.log("Code = " + r.responseCode); console.log("Response = " + r.response); console.log("Sent = " + r.bytesSent); var str=r.response; var n=str.indexOf("image_name="); var n_l=str.indexOf(".png"); if(r.responseCode){ var imgg=str.substr(n+11,n_l+1); var index1=imgg.indexOf("2"); var real_img=imgg.substr(index1,index1+18); window.location="property_step2.php?image_name="+real_img+"&platform=<?php
echo $plat.'&rid='.$rid;?>"; } }
function fail(error) { switch (error.code) { case FileTransferError.FILE_NOT_FOUND_ERR: alert("Photo file not found"); break; case FileTransferError.INVALID_URL_ERR: alert("Bad Photo URL"); break; case FileTransferError.CONNECTION_ERR: alert("Connection error"); break; } } </script>
the html code:
Property Main Image *
Capture Photo
Or
From Photo Library<!--<img style="display:none;width:200;height:150px;" id="smallImage" src="" />--> <form name="myform" action="" method="POST" id="hide_form" > <img style="display:none;width:200;height:150px;" id="smallImage"
src="" /> Please Capture Image in Landscape Mode. -->
</div>
the logcat says:
E/FileTransfer(24735): java.net.ProtocolException: expected 13 bytes but received 16384 E/FileTransfer(24735): at com.squareup.okhttp.internal.http.HttpTransport$FixedLengthOutputStream.write(HttpTransport.java:226) E/FileTransfer(24735): at com.squareup.okhttp.internal.FaultRecoveringOutputStream.write(FaultRecoveringOutputStream.java:60) E/FileTransfer(24735): at org.apache.cordova.FileTransfer$1.run(FileTransfer.java:425) 09-08 08:04:07.953: E/FileTransfer(24735): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 09-08 08:04:07.953: E/FileTransfer(24735): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 09-08 08:04:07.953: E/FileTransfer(24735): at java.lang.Thread.run(Thread.java:856) 09-08 08:04:07.953: E/FileTransfer(24735): Failed after uploading 243 of 256 bytes.
menifest :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="au.com.appyagents" android:versionCode="1" android:versionName="1.0" >
-->
<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.VIBRATE" /> <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_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.RECORD_VIDEO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.BROADCAST_STICKY" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application android:debuggable="true" android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden" android:theme="@style/AppTheme" > <activity android:name="au.com.appyagents.MainApp" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name"> </activity> <activity android:name="au.com.appyagents.Splash" android:label="@string/title_activity_splash" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="au.com.appyagents.MainActivity" android:label="@string/title_activity_main" > </activity> <receiver android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
</application>