我正在使用 phonegap 2.9 开发我的 android 应用程序。从手机图库上传图片时出现错误代码 3。
我一直在关注http://docs.phonegap.com/en/2.9.0/cordova_camera_camera.md.html#Camera中提供的 phonegap 文档,并且为了上传图片,我一直在关注文件传输 phonegap 文档 http: //docs.phonegap.com/en/2.9.0/cordova_file_file.md.html#File。
从手机图库中选择图像时我没有问题。但是当我将它上传到网络服务器时,我在警告框中出现错误。
从手机图库到服务器的图像选择和上传在 samsung grand、samsung s、htc 中完全正常,但在 samsung s3 和 s4 中进行测试时给出错误代码 3 即连接错误。
大多数情况下,在捕获图像或从图库中选择图像后,应用程序会重新启动。
任何形式的帮助将不胜感激。
js代码:
<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>
html代码:
属性主图像 *
捕获照片
或
从照片库<!--<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="" /> 请以横向模式拍摄图像。 -->
</div>
logcat 说:
E/FileTransfer(24735): java.net.ProtocolException: 预期 13 个字节但收到 16384 E/FileTransfer(24735): at com.squareup.okhttp.internal.http.HttpTransport$FixedLengthOutputStream.write(HttpTransport.java:226) E /FileTransfer(24735):在 com.squareup.okhttp.internal.FaultRecoveringOutputStream.write(FaultRecoveringOutputStream.java:60) E/FileTransfer(24735):在 org.apache.cordova.FileTransfer$1.run(FileTransfer.java:425) 09-08 08:04:07.953: E/FileTransfer(24735): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 09-08 08:04:07.953: E/FileTransfer(24735): 在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:电子/文件传输(24735):上传 243 个字节(共 256 个字节)后失败。
清单:
<?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>