1

我正在捕获图像并将其保存到 SD 卡中,一切正常。但是我需要发送这个图像可能是一个字符串或base64或utf-8以及一些其他数据,如用户名、密码。告诉我如何处理该数据变量并使用http请求将其发送到服务器

@Override
public void onPictureTaken(byte[] data, Camera camera) {

    File pictureFileDir = getDir();

    if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) 
    {
        Toast.makeText(context, "Can't create directory to save image.",Toast.LENGTH_LONG).show();
        return;
    }


    String photoFile = "MyPicture.jpg";
    String filename  = pictureFileDir.getPath() + File.separator + photoFile;
    File pictureFile = new File(filename);
    //Need to send this data to server     
    data.toString();

    try 
    {
        FileOutputStream fos = new FileOutputStream(pictureFile);
        fos.write(data);
        fos.close();
        Toast.makeText(context, "New Image saved:" +        photoFile,Toast.LENGTH_LONG).show();
    }
    catch (Exception error) 
    {
        Toast.makeText(context, "Image could not be saved.",Toast.LENGTH_LONG).show();
    }
    public void postData(String emailId,String passwrd) {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://XXXXAAAA.com/login");

            try {
                // Data that I am sending
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("mb_code", emailId));
                nameValuePairs.add(new BasicNameValuePair("pwd", passwrd));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                // Execute HTTP Post Request
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                responseBody = EntityUtils.toString(response.getEntity());

                Log.d("result", responseBody);
            } 
            catch (Throwable t ) {
                //Toast.makeText( getApplicationContext(),""+t,Toast.LENGTH_LONG).show();
                Log.d("Error Time of Login",t+"");
            } 
        }
           }
4

0 回答 0