1

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>

mahima
  • 43
  • 8
  • Post some code, that will be helpful. – Amol Chakane Aug 23 '13 at 11:45
  • i have edited with code in it – mahima Aug 30 '13 at 08:56
  • Have a look at my answer to this question [here](http://stackoverflow.com/questions/17445840/want-to-upload-a-pic-to-the-server-using-phonegap-in-android/17447249#17447249). Hope that helps – Amol Chakane Aug 30 '13 at 09:54
  • i have almost done the same thing as you have said in there.The image selection and upload from the phone's gallery to the server works completely fine in samsung grand but gives error code 3 i.e connection error while testing in samsung s3 and s4. – mahima Sep 01 '13 at 03:46
  • What does the logcat say? – Amol Chakane Sep 02 '13 at 09:43
  • check this [answer](http://stackoverflow.com/questions/11754299/phonegap-android-unable-to-upload-image-using-filetransfer/14220608#14220608). You may have to resolve the uri before uploading. – Davor Zlotrg Sep 03 '13 at 07:52
  • Thank @DZL i have resolved the uri as a filename but the problem is the same. moreover i'm not being able to select the image or capture the image.my app crashes or restarts when i finsih selecting/ capturing the image – mahima Sep 03 '13 at 09:51
  • E/FileTransfer(24735): java.net.ProtocolException: expected 13 bytes but received 16384 at com.squareup.okhttp.internal.http.HttpTransport$FixedLengthOutputStream.write(Ht‌​tpTransport.java:226)at org.apache.cordova.FileTransfer$1.run(FileTransfer.java:425) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) at java.lang.Thread.run(Thread.java:856) E/FileTransfer(24735): Failed after uploading 243 of 256 bytes. @AmolChakane – mahima Sep 04 '13 at 02:39
  • Depending on error log, it seems the issue is with server side script. Also as you said it happens on some devices, I would like to have a look at **AndroidaManifest** code. – Amol Chakane Sep 04 '13 at 04:57
  • @AmolChakane i have edited with manifest code in the question – mahima Sep 04 '13 at 05:55
  • Try this: Add `` line before first ` – Amol Chakane Sep 04 '13 at 06:12
  • yes i have done that.@AmolChakane but the problem is still the same. – mahima Sep 05 '13 at 07:25
  • is there any other solutions @AmolChakane ?? – mahima Sep 14 '13 at 05:29
  • If you can share complete code on git, I can spend some time to figure out the issue. – Amol Chakane Sep 16 '13 at 05:26
  • Thank you all of you .I have successfully uploaded by changing the file uri to data when selecting the photo – mahima Sep 27 '13 at 09:27

0 Answers0