2

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

4

3 回答 3

0

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

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
于 2013-10-03T09:47:12.773 回答
0

此处发布了一个非常好的代码:https ://stackoverflow.com/a/11826317/1234007 。您必须使用多部分通过 POST 发送数据。PHP在处理方面非常好。查看 PHP 的官方文档以获取处理具有文件和图像的 POST 数据的工作代码http://php.net/manual/en/features.file-upload.php

一切顺利

于 2013-10-03T09:51:23.023 回答
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;
        }
}
于 2013-10-03T09:59:31.180 回答