Upload picture to server

前端 未结 1 1282
野趣味
野趣味 2021-01-16 18:08

I\'ve googled a lot but it doesn\'t work. I found a lot of sites with information but by all the sites my app crashed. The picture that I want to open is: \"lastfile.png\".

1条回答
  •  爱一瞬间的悲伤
    2021-01-16 18:51

    The way I did it was to compress the img in to a type of string then send it as name value pair then decode the string on server end using php.

    Bitmap bitmapOrg = BitmapFactory.decodeResource("your image path on device");

            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
            byte [] ba = bao.toByteArray();
            String ba1= Base64.encodeToString(ba, 0);
             ArrayList nameValuePairs = new
    ArrayList();
                nameValuePairs.add(new BasicNameValuePair("image",ba1));
    
    try{
    
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://your_url/sink.php");
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();
              }catch(Exception e){
    
                    Log.e("log_tag", "Error in http connection "+e.toString());
              } 
    

    SINK.PHP

    ';
    

    ?>

    0 讨论(0)
提交回复
热议问题