在Android上将图片上传到Amazon S3时阻止线程 [英] Block thread while uploading picture to Amazon S3 on Android

查看:73
本文介绍了在Android上将图片上传到Amazon S3时阻止线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以阻止当前线程并等待图片上传到S3?

Is there a way to block the current thread and wait while the picture is uploaded to S3?

我上传到S3的代码已经在Android服务中的doInBackground()方法中运行了(必须按特定顺序进行其他网络调用),因此它不在主UI线程中.我只想知道什么时候上传完成,所以我可以取消通知并向用户显示所有内容都已上传/完成.

My code for uploading to the S3 is already running in a Android service in its doInBackground() method (with other network calls that have to be in specific order), so it's off the main UI thread. I just want to know when the uploading is done, so I can dismiss notification and show to user that everything is uploaded/done.

我正在使用SDK v2,如下所示:

I'm using SDK v2, like this:

TransferUtility transferUtility = new TransferUtility(s3, ctx);
TransferObserver obs = transferUtility.upload(
            S3BucketNamePhotoTEST,
            imageId + ".jpg",
            file, new ObjectMetadata());
obs.setTransferListener(new UploadListener(dbHandler, ctx, imageId));

其中UploadListener是实现TransferListener的类,在onStateChanged()方法中,我们可以检测到何时完成上传:

Where UploadListener is a class implementing TransferListener, where in onStateChanged() method we can detect when the upload is finished:

public void onStateChanged(int id, TransferState state) {
        if (state == TransferState.COMPLETED) {
        //do something here!
        }
}

那不是我所需要的,因为当onStateChange()回调触发时,我的服务调用将被长时间完成.

That's not what I need, because my service call will be long done when that onStateChange() callback fires.

但是查看SDK v1(现在已不推荐使用),有一种方法可以阻止它,方法是:

But looking at SDK v1 (that's now mostly deprecated), there was a way to block it, with:

TransferManager tx = new TransferManager(
           credentialProviderChain.getCredentials());
Upload myUpload = tx.upload(myBucket, myFile.getName(), myFile);
...
// Or you can block the current thread and wait for your transfer to
// to complete. If the transfer fails, this method will throw an
// AmazonClientException or AmazonServiceException detailing the reason.
myUpload.waitForCompletion();

这个waitForCompletion()方法正是我所需要的,但是无法使用较新的API来实现.

This waitForCompletion() method is just what I need, but can't get to it with the newer API.

有解决方案吗?

推荐答案

最后,我在这里从Amazon开发人员那里得到了答案-> https://github.com/aws/aws-sdk-android/issues/91

Finally, I got the answer from Amazon developer here -> https://github.com/aws/aws-sdk-android/issues/91

这篇关于在Android上将图片上传到Amazon S3时阻止线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆