我有一个Android应用程序将数据发布到php webservice,如果它从Android应用程序发布到php webserive它不能按预期工作,如果相同的数据从HTML页面发布到php webservice它工作正常。
php脚本中不起作用的部分如下:
function makeThumb($src, $dest, $desired_width) {
/* read the source image */
$imgsrc = explode('.',$src,-1);
$imgdest = explode('.',$dest,-1);
$dest = $imgdest[0].'Thumb.png';
$source_image = imageConvert($src, $dest,0);
list($width, $height, $img_type, $img_attr) = getimagesize($dest);
//$width = imagesx($source_image);
//$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
imagealphablending($virtual_image, false);
/* copy source image at a resized size */
$source_image = imagecreatefrompng($source_image);
//imagealphablending($source_image, false);
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagealphablending($virtual_image,false);
imagesavealpha($virtual_image,true);
imagepng($virtual_image, $dest);
}
从android发布到php文件的代码是:
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
// ::
File tempFile = getTempFile();
String filePath = Environment.getExternalStorageDirectory() + "/"
+ TEMP_PHOTO_FILE;
System.out.println("path " + filePath);
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
// Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); // compress to
// which format
// you want.
byte[] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//nameValuePairs.add(new BasicNameValuePair("action", "uploadImage"));
nameValuePairs.add(new BasicNameValuePair("image", image_str));
nameValuePairs.add(new BasicNameValuePair("sociochatU",
IMService.USERNAME));
nameValuePairs.add(new BasicNameValuePair("sociochatP",
IMService.password));
nameValuePairs.add(new BasicNameValuePair("mpf",
"true"));
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
SocketOperator.AUTHENTICATION_SERVER + "includes/inc.upload.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
final String the_string_response = convertResponseToString(response);
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(UserProfile.this,
"Response " + the_string_response,
Toast.LENGTH_LONG).show();
}
});
} catch (final Exception e) {
System.out.println("Error in http connection "
+ e.toString());
}
}
});
t.start();
}
我在HTML中用来发布到同一个php文件的代码是followig:
<form method="POST" action="SocketOperator.AUTHENTICATION_SERVER & "includes/inc.upload.php" target="_blank">
<input name="sociochatU" id="sociochatU" value=""/><br/>
<input name="sociochatP" id="sociochatP" value=""/><br/>
<input name="mpf" id="mpf" value="true"/><br/>
<textarea name="image" id="base64"></textarea><br/>
<input type="submit" name="Submit" id="Submit" value="Submit"/><br/>
我不知道问题是什么,有什么建议吗?
答案 0 :(得分:1)
在此处发布代码后立即发现问题。
我注意到android应用程序正在将图像压缩为PNG图像,并且在服务器端它将其指定为JPG文件,更改了php文件上的代码,而不是分配PNG扩展名和瞧。有效。