为什么不能将位图转换为字节数组并将其传递给服务器。这是向服务器发送位图的代码。
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// Preview_bitmap is the one you need to send to the server. I'm compressing here and sending this to server:
preview_bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
// constant.uploadImagesAPI is the your server URL :
HttpPost postRequest = new HttpPost(Constant.uploadImagesAPI
+ Constant.mDeviceID);
ByteArrayBody bab = new ByteArrayBody(data, ".jpg");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("image", bab);
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder mUploadResponse = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
mUploadResponse = mUploadResponse.append(sResponse);
}
JSONObject mUploadResponseObject = new JSONObject(
mUploadResponse.toString());
mUploadResponseObject.getJSONArray("response");
try {
JSONArray jsonArray = mUploadResponseObject
.getJSONArray("response");
for (int i = 0; i < jsonArray.length(); i++) {
uploadStatus = jsonArray.getJSONObject(i)
.getJSONObject("send").getString("message");
uploadPhotoID = jsonArray.getJSONObject(i)
.getJSONObject("send").getString("id");
Constant.imageUploadedFlag = true;
}
} catch (Exception e) {
serverUploadException = true;
}
} catch (Exception e) {
}
// PHP code :
$to = $_REQUEST['deviceid'];
//$timestamp = $_REQUEST['timestamp'];
$path=PATH.'upload/';
//$path1=PATH.'newupload/';
//$name = $_FILES['image']['name'];
//$str=explode(".",$name);
//$imname=$str[0];
$filename=upload::save($_FILES['image']);
$file_name1= basename($filename);
$docroot= $_SERVER['DOCUMENT_ROOT'];
//$root=$docroot.'/newupload/';
$roots=$docroot.'/upload/';
$url = $path.$file_name1;
$send = $this->api->upload_images($to,$url);
if($send)
{
$json_response[] = array("send" =>
array("id"=> $send,
"message"=>"Message Sent Successfully",
"status"=>1));
}
echo json_encode(array ('response' =>$json_response));
break;
试试这个。