Upload picture to server

前端 未结 1 1280
野趣味
野趣味 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<NameValuePair> nameValuePairs = new
    ArrayList<NameValuePair>();
                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

    <?php
    
    $base=$_REQUEST['image'];
    $name=$_REQUEST['name'];
    echo $base;
    // base64 encoded utf-8 string
    $binary=base64_decode($base);
    // binary, utf-8 bytes
    header('Content-Type: bitmap; charset=utf-8');
    
    
    $file = fopen(name, 'wb');
    fwrite($file, $binary);
    fclose($file);
    echo '<img src='+name+'>';
    

    ?>

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