将图片上传到服务器

时间:2012-11-14 17:18:33

标签: java android file-upload

我已经google了很多但它不起作用。我发现很多网站都有信息,但是我的应用程序崩溃了所有网站。我要打开的图片是:“lastfile.png”。它存储在内部存储中,因此我用openFileInput(“lastfile.png”)打开它;

我是在AsyncTask中完成的。

到目前为止,这是我的代码:

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();       
        }


    }

2 个答案:

答案 0 :(得分:1)

有许多类似的问题,这对我来说是完美的答案。

看看这个:

Link 1

android-upload-image-to-server

complete answer

答案 1 :(得分:0)

我这样做的方法是将img压缩为一种字符串,然后将其作为名称对发送,然后使用php解码服务器端的字符串。

Bitmap bitmapOrg = BitmapFactory.decodeResource(“你设备上的图像路径”);

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

&GT;