AWS S3 TransferListener接收重复的COMPLETED ID

时间:2016-04-15 06:50:24

标签: android amazon-web-services file-upload amazon-s3

我从https://github.com/awslabs/aws-sdk-android-samples下载了S3TransferUtilitySample。然后在其UploadActivity中添加以下代码以进行测试:

    private void uploadFiles() {
        File dir = new File("/storage/sdcard0/DCIM");
        File[] files = dir.listFiles();
        for(File file : files) {
            uploadAFile(file);
        }
    }

    private void uploadAFile(File file) {
        if(file.isDirectory() || !file.exists()) {
            return;
        }

        TransferObserver observer = transferUtility.upload(Constants.BUCKET_NAME, file.getName(), file);
        observer.setTransferListener(new UploadListener());
    }

我的UploadListener如下所示:

private class UploadListener implements TransferListener {

        // Simply updates the UI list when notified.
        @Override
        public void onError(int id, Exception e) {
            Log.e(TAG, "Error during upload: " + id, e);
        }

        @Override
        public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
            Log.d(TAG, String.format("onProgressChanged: %d, total: %d, current: %d",
                    id, bytesTotal, bytesCurrent));
        }

        @Override
        public void onStateChanged(int id, TransferState newState) {
            if(newState == TransferState.COMPLETED) {
                Log.d(TAG, "file transfer completed:" + id);
            }
        }
    }

在某些Button Click侦听器中调用uploadFiles()。然后我得到了以下日志:

file transfer completed:21
file transfer completed:26
file transfer completed:44
file transfer completed:44
file transfer completed:46
file transfer completed:46
file transfer completed:47
file transfer completed:47
file transfer completed:48
file transfer completed:48

你看,有些ID是重复的!这是有线的!你知道这是什么问题吗?帮我。 (注意:完成后我需要使用id做更多的事情)

1 个答案:

答案 0 :(得分:0)

那是因为示例应用程序的设置。它有一个跟踪传输进度的列表视图。它会在页面加载时为每个正在进行的传输附加一个监听器。并注册一个相同的UploadListener。看起来你的触发器被触发了两次,但实际上有一个来自列表视图。希望解释一下。