【问题标题】:How to send more than one image to server如何将多个图像发送到服务器
【发布时间】:2014-04-21 07:28:01
【问题描述】:

当我上传多于一张图片时,我的代码正在处理一张图片,它向我显示错误。

谁能帮我解决这个问题?

使用下面的代码我可以上传单张图片,但我想上传多张图片,所以请帮助我如何上传图片。 谢谢

这是我的代码

  String image_path=imageUrl1;
      GetData(myFiles);         

}


        });

}


public void GetData(String myFiles)
{

        String image_retrive_url="http://tradewale.com/open/tradedata.php?submitType=PostAd";

        pDialog = new ProgressDialog(PostAddActivity.this);
        pDialog.setMessage(" Sending...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();




    Bitmap bitmap = BitmapFactory.decodeFile(myFiles);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream);
    byte [] byte_arr = stream.toByteArray();
    String image_str = Base64.encodeBytes(byte_arr);
    final ArrayList<NameValuePair> nameValuePairs = new  ArrayList<NameValuePair>();

    String Title = title.getText().toString();
    String Description = description.getText().toString().trim();
    String PhoneNumber = phonenumber.getText().toString();
    String Email = email.getText().toString();
    String Price = price.getText().toString();


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

    nameValuePairs.add(new BasicNameValuePair("photo_url", image_retrive_url));



    Thread t = new Thread(new Runnable() 
    {

   @Override
   public void run() {
         try{
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://tradewale.com/open/tradedata.php?submitType=PostAd");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                final String the_string_response = convertResponseToString(response);


            }
         catch(final Exception e)
            {
                 runOnUiThread(new Runnable() 
                 {

                   @Override
                   public void run() 
                   {
                       Toast.makeText(PostAddActivity.this, "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();                              
                   }
               });
                  System.out.println("Error in http connection "+e.toString());
            }  
   }
       });
   t.start();
    }

【问题讨论】:

    标签: php android imageview multiprocessing bitmapfactory


    【解决方案1】:

    在网络服务中发送更多图像。您还必须更改webservice。在服务部分添加one more参数。这样您就可以一次发送多个图像。

    获取第二张图片的base64字符串,如e.g. img_str2

    喜欢:

    String image_str = Base64.encodeBytes(byte_arr);
    String image_str2 = Base64.encodeBytes(byte_arr2);
    

    并在服务中命名新参数,如image2

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

    这里image 和image2 是2 diff。图像参数。这些参数名称也必须是define in your service side code.. 这样服务代码才能保存您的图像。

    【讨论】:

    • 这样我创建了四种不同的方法
    • 不需要四种不同的方法..只需添加更多参数。
    • 你能告诉我怎么做
    • 在 webserivce 代码中,您可以为图像请求更多参数,而在您的 android 代码中,您必须添加更多参数。查看我编辑的答案。
    • String [] images=image_path.split(","); for (String image : images) { 位图位图 = BitmapFactory.decodeFile(image); ByteArrayOutputStream 流 = 新的 ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 30, 流); byte [] byte_arr = stream.toByteArray(); image_str = Base64.encodeBytes(byte_arr);多个图像=流+“,”; }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 2020-05-07
    • 1970-01-01
    相关资源
    最近更新 更多