Android将图像和其他信息上传到httpServer

时间:2013-03-18 07:05:20

标签: android

我想将位图图像和其他信息(如userid和username)传递给php服务器。 这是我的php服务器端API ...

     $user_id=$app->request()->post('user_id'); 
     $username=$app->request()->post('username'); 

     $target_path="./images/";
     $s_char = array ("'");
     $target_path = $target_path . basename($_FILES['uploadedfile']['name']);
     //Get the uploaded file information
    $name_of_uploaded_file = basename($_FILES['uploadedfile']['name']);   
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) 
    {
   echo "image uploaded====>".$name_of_uploaded_file;
   $photo_url='http://192.168.1.239/taxibuddy/api/images'.$name_of_uploaded_file;
   $photo_url=str_replace($s_char,"`",$photo_url); 
   }

我尝试使用thing链接 http://reecon.wordpress.com/2010/04/25/uploading-files-to-http-server-using-post-android-sdk/ 但这要求指定文件名路径,我想将位图作为参数传递。

1 个答案:

答案 0 :(得分:1)

为什么不能将位图转换为字节数组并将其传递给服务器。以下是将位图发送到服务器的代码。

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; 

试试这个。