将图像逐个上传到服务器 - Android

时间:2015-07-15 13:11:51

标签: android image list loops file-upload

我想知道如何才能将图像列表上传到服务器,但最重要的是逐一上传。

Bellow是包含带有图像列表的地图的代码,我想循环浏览列表并一次上传一张图片。

如果可能,不使用计时器。我真的很感激任何建议。

sendOrder.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {

    for (Map.Entry<String,String> entry : imgPathAndNameMap.entrySet()) {
                                uploadPhoto(entry.getKey(), entry.getValue());
                            }
                     }
                  }
              });


public void uploadPhoto(String imgName, String imgPath){

        final int DEFAULT_TIMEOUT = 30000;
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(DEFAULT_TIMEOUT);

        RequestParams params = new RequestParams();

        //check image size
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;
        Bitmap bp = BitmapFactory.decodeFile(imgPath, opts);

        //get the original size
        int orignalHeight = opts.outHeight;
        int orignalWidth = opts.outWidth;

        Log.w("", orignalHeight + "x" + orignalWidth);

        if(orignalHeight > 2000 || orignalWidth > 2000){
            byte[] bitmapdata = resize(imgPath, orignalHeight, orignalWidth, opts, bp);
            params.put("zipFile", new ByteArrayInputStream(bitmapdata), imgName);
        } else {
            File theZip = new File(imgPath);
            try {
                Log.w("", imgPath );
                params.put("zipFile", theZip);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        params.put("name", name.getText());


            client.post("http://www.lab.com/upload-image.php", params,
                    new AsyncHttpResponseHandler() {

                        @Override
                        public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                            if (arg0 == 0) {

                            }
                        }

                        @Override
                        public void onProgress(int position, int length) {
                            try {
                                ((PreloaderDialog) progressDialog).passValues(
                                        position, length);
                            } catch (Exception e) {

                            }

                        }

                        @Override
                        public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                            Toast.makeText(context, "Multumim pentru Comanda!", Toast.LENGTH_LONG).show();


                        }
                    });
        }

2 个答案:

答案 0 :(得分:1)

要逐个发布图像,您可能需要使用同步请求,但您可以在UI线程上执行此操作。所以你应该在背景中这样做。例如,您可以使用AsyncTask

答案 1 :(得分:1)

由于缺乏更好的解决方案,我设置了loopj Async Http Client的onSuccess()方法,以在上传图片后自动触发父方法。同时,我在我用来存储每个图像的路径的数组中增加即将到来的文件的位置。

效果很好,希望帮助其他人解决同样的问题。

public void uploadPhoto(){           

        String iPath = files[position];
        String iName = imgName[position];

        final int DEFAULT_TIMEOUT = 30000;
        AsyncHttpClient client = new AsyncHttpClient();
        client.setTimeout(DEFAULT_TIMEOUT);


       File imageFile = new File(imagePath);
         try {
              params.put("imageFile", imageFile);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }    

     params.put("position", position);        
     params.put("name", name.getText());


      client.post("http://www.yoursite.com/upload-img.php", params,
              new AsyncHttpResponseHandler() {

                   @Override
                    public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                            uploadPhoto();
                        }

                  @Override
                  public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                     position ++;
                     if(position < files.length) {
                           uploadPhoto();
                          } else {
                           progress.dismiss();
                           Toast.makeText(context, "Thanks for your order!",    Toast.LENGTH_LONG).show();
                                finish();
                            }

                        }
                    });
        }