duangejian6657 2013-12-18 08:03
浏览 92
已采纳

如何将捕获的图像发送到服务器?

Hi i am developing an app which includes camera images, i need to send images while capturing to php server, in our settings there is boolean option as "AutoUploadPic" when we set the switch to yes, then only it should upload to server, if it set to no it should not upload. can you please help me out from this.

The following is iphone code for above query, can i get as the same for android.

  • 写回答

2条回答 默认 最新

  • doujuyang1764 2013-12-18 08:10
    关注

    Why can't you convert your bitmap into byte array and pass that to your server. Here is the code to send a bitmap to the server?

    Please do these stuffs in AsyncTask.

    public class UploadPicture extends AsyncTask<Void, Void, Void> {
    
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            // show your progress bar
    
        }
    
        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
    
            // wrap up all your upload code here..
    
            return null;
        }
    
        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            // stop your progress bar
        }
    }
    

    Call this class after checking your boolean :

     if(AutoUploadPic)
    {
    new UploadPicture().execute();
    }
    else
    {
    // Your code here..
    }
    

    // do the following in the doInBackground Class :

        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; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题