Android MultipartUploadRequest在异步任务doInBackground中不起作用

时间:2019-01-19 13:48:35

标签: java android

我正在尝试构建一个应用程序,该应用程序可以拍摄照片并以异步功能对其进行处理并上传。这是我的代码的样子:

Camera.PictureCallback jpegCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) { 
                if(photoHasToBeProcessed){
                    DoAsyncTask task = (DoAsyncTask) new DoAsyncTask().execute(MainActivity.this,data);
                }else{
                    new MultipartUploadRequest(MainActivity.this, uploadID, serverUrlString)
                  .setAutoDeleteFilesAfterSuccessfulUpload(true)
                  .setMethod("POST")
                  .addHeader("x-amz-acl", "bucket-owner-full-control")
                  .addFileToUpload(photo.getAbsolutePath(), "file")
                  .addParameter("key", key)
                  .addParameter("acl", "bucket-owner-full-control")
                  .addParameter("success_action_status", "201")
                  .addParameter("faces", "{}")
                  .setNotificationConfig(uploadNotificationConfig)
                  .setMaxRetries(2)
                  .startUpload();
                }

这在异步类中

public class ScanFacesAsyncTask extends AsyncTask<Object, Void, Long> {
   protected Long doInBackground(Object... params) {
      if(processImage()){
         new MultipartUploadRequest(ctx, uploadID, serverUrlString)
          .setAutoDeleteFilesAfterSuccessfulUpload(true)
          .setMethod("POST")
          .addHeader("x-amz-acl", "bucket-owner-full-control")
          .addFileToUpload(photo.getAbsolutePath(), "file")
          .addParameter("key", key)
          .addParameter("acl", "bucket-owner-full-control")
          .addParameter("success_action_status", "201")
          .addParameter("data", data.toString())
          .setNotificationConfig(uploadNotificationConfig)
          .setMaxRetries(2)
          .startUpload();
      }

  }
}

现在,每当我在UI线程上运行multipartuploadreq时,它就可以正常工作,但是执行该操作时,它不会上载,也不会显示任何错误。

1 个答案:

答案 0 :(得分:0)

尝试一下:

    protected String doInBackground(String... args) {

        try {

            new MultipartUploadRequest(MainActivity.this, uploadID, serverUrlString)
                    .setAutoDeleteFilesAfterSuccessfulUpload(true)
                    .setMethod("POST")
                    .addHeader("x-amz-acl", "bucket-owner-full-control")
                    .addFileToUpload(photo.getAbsolutePath(), "file")
                    .addParameter("key", key)
                    .addParameter("acl", "bucket-owner-full-control")
                    .addParameter("success_action_status", "201")
                    .addParameter("data", data.toString())
                    .setNotificationConfig(uploadNotificationConfig)
                    .setMaxRetries(2)
                    .startUpload();


        } catch (final Exception exc) {

            runOnUiThread(new Runnable() {
                public void run() {
                    Toast.makeText(this, exc.getMessage(), Toast.LENGTH_LONG).show();
                }
            });

        }

        return null;

    }

    protected void onPostExecute(String file_url) {
        //Do something
    }