【问题标题】:In Android, how to upload an image and string data to a website在 Android 中,如何将图像和字符串数据上传到网站
【发布时间】:2013-10-03 09:31:50
【问题描述】:

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

【问题讨论】:

  • 请任何人都可以逐步解释我们上传时后台发生的情况。作为前任:-“outPutStream.write();”将上传字节或发生什么。以及客户端期望的 php 文件。如果你能解释完事情,也许我可以写我自己想要的代码。

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


【解决方案1】:

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

MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

【讨论】:

  • 请阅读问题,在得到明确的想法后发布有用的答案。如果不是,请不要发布任何内容
【解决方案2】:

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

一切顺利

【讨论】:

    【解决方案3】:
    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;
            }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      • 2019-03-22
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      相关资源
      最近更新 更多