在Android中,如何将图像和字符串数据上传到网站

时间:2013-10-03 09:31:50

标签: android performance android-layout android-intent android-networking

我在android中创建应用程序,在我的应用程序中,我需要为用户提供上传带有GPS位置数据的图像的功能。在Google上进行了多次搜索后,我无法找到将图像从Android上传到服务器的任何工作代码。我想使用'HttpURLConnection'并希望在服务器端进行PHP编码。任何正文都可以为我提供一个示例代码,包含java类和php编码的解释。

3 个答案:

答案 0 :(得分:0)

使用MultipartEntity将图像上传到服务器。

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

答案 1 :(得分:0)

这里发布了一个非常好的代码:https://stackoverflow.com/a/11826317/1234007。您必须使用多部分通过POST发送数据。 PHP非常适合处理。查看PHP的官方文档,了解处理包含文件和图像的POST数据的工作代码http://php.net/manual/en/features.file-upload.php

一切顺利

答案 2 :(得分:0)

For android :[enter link description here][1]

mport java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.Toast;

public class UploadImage extends Activity {
    InputStream inputStream;
        @Override
    public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.activity_image_upload);

            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);         
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
            byte [] byte_arr = stream.toByteArray();
            String image_str = Base64.encodeBytes(byte_arr);
            ArrayList<namevaluepair> nameValuePairs = new  ArrayList<namevaluepair>();

            nameValuePairs.add(new BasicNameValuePair("image",image_str));

            try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://10.0.0.23/Upload_image_ANDROID/upload_image.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                String the_string_response = convertResponseToString(response);
                Toast.makeText(UploadImage.this, "Response " + the_string_response, Toast.LENGTH_LONG).show();
            }catch(Exception e){
                  Toast.makeText(UploadImage.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
                  System.out.println("Error in http connection "+e.toString());
            }
        }

        public String convertResponseToString(HttpResponse response) throws IllegalStateException, IOException{

             String res = "";
             StringBuffer buffer = new StringBuffer();
             inputStream = response.getEntity().getContent();
             int contentLength = (int) response.getEntity().getContentLength(); //getting content length…..
             Toast.makeText(UploadImage.this, "contentLength : " + contentLength, Toast.LENGTH_LONG).show();
             if (contentLength < 0){
             }
             else{
                    byte[] data = new byte[512];
                    int len = 0;
                    try
                    {
                        while (-1 != (len = inputStream.read(data)) )
                        {
                            buffer.append(new String(data, 0, len)); //converting to string and appending  to stringbuffer…..
                        }
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                    try
                    {
                        inputStream.close(); // closing the stream…..
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                    res = buffer.toString();    


             Toast.makeText(UploadImage.this, "Result : " + res, Toast.LENGTH_LONG).show();
                    //System.out.println("Response => " +  EntityUtils.toString(response.getEntity()));
             }
             return res;
        }
}