【问题标题】:Upload images to server one by one - Android一张一张上传图片到服务器 - Android
【发布时间】:2015-10-04 13:13:25
【问题描述】:

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

下面是包含带有图像列表的地图的代码,我想遍历列表并一次上传一张图像。

如果可能,不使用计时器。如果有任何建议,我将不胜感激。

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();


                        }
                    });
        }

【问题讨论】:

  • 最好的选择是 Volley - 谷歌一下。
  • 你可以再次使用AsyncTask,OnPost方法,你可以用不同的图像启动asyncTask,而无需与UI线程交互。否则你可能会遇到UI冻结问题。

标签: android image list loops file-upload


【解决方案1】:

要一张一张地发布您的图片,您可能需要使用同步请求,但您可以在 UI 线程上执行此操作。所以你应该在后台这样做。例如,您可以使用AsyncTask

【讨论】:

    【解决方案2】:

    由于没有更好的解决方案,我设置了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();
                                }
    
                            }
                        });
            }
    

    【讨论】:

      【解决方案3】:

      尝试使用Handler.postDelayed()

      【讨论】:

      • 你应该建议去哪里
      猜你喜欢
      • 2019-03-07
      • 2013-01-12
      • 1970-01-01
      • 2021-12-01
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 2012-03-11
      相关资源
      最近更新 更多