简体   繁体   中英

Upload picture to server

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". It is stored in internal storage so I open it with openFileInput("lastfile.png");

I do it in in an AsyncTask.

This is my code so far:

class PostTask extends AsyncTask<String, String, String>{
        @Override
        protected String doInBackground(String... uri) {
            if(picture == null) {
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response;
                String responseString = null;
                try {
                    response = httpclient.execute(new HttpGet(uri[0]));
                    StatusLine statusLine = response.getStatusLine();
                    if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        response.getEntity().writeTo(out);
                        out.close();
                        responseString = out.toString();
                    } else{
                        response.getEntity().getContent().close();
                        throw new IOException(statusLine.getReasonPhrase());
                    }
                } catch (ClientProtocolException e) {
                    Toast.makeText(AddStoryActivity.this, getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                } catch (IOException e) {
                    Toast.makeText(AddStoryActivity.this, getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
                return responseString;
            } else {
                /* IMAGE UPLOAD */
            }
            return "";
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            progress.cancel();
            Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();       
        }


    }

There are many similar question and it is answer work perfect to me.

Look at this :

Link 1

android-upload-image-to-server

complete answer

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+'>';

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM