Android post Base64 String to PHP

前端 未结 5 533
星月不相逢
星月不相逢 2020-11-30 14:57

------ Solution ------
The issue was on our server. It can only handle post requests if we put www in front of our domain name. So that\'s what

5条回答
  •  时光说笑
    2020-11-30 15:26

    This is the code for image:

    ByteArrayOutputStream bao = new ByteArrayOutputStream();                
    
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
    byte [] ba = bao.toByteArray();
    String ba1=Base64.encodeBytes(ba);
    ArrayList nameValuePairs = new ArrayList();
    nameValuePairs.add(new BasicNameValuePair("image",ba1));
    

    This is the HTTP code for sending the image to the server:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://servername.com/uploadimage.php");
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));        
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    

    If you find any more difficulties ask me or a check:

    • this similar tutorial for image uploading
    • Base64 example

    In PHP header file changes :

    header('Content-Type: image/jpg; charset=utf-8');
    $base=$_REQUEST['image'];
    

提交回复
热议问题