我正在尝试从 s3 下载并更新进度条。我设置了 TransferUtility、TransferObserver 和 TransferListener。
问题是,当文件下载时,它很少更新进度。
对于 1.6mb (1665824) 文件,它将输出 0,然后 30 秒后输出 1050264,然后 30 秒后输出 1665824,然后重复自身并再次输出 1665824。
所以基本上对于用户来说,下载看起来像是卡住了或者充其量是不稳定的。
我还尝试将其放入循环中并每隔 100 毫秒检查一次值,但它只在前 30 秒返回 0,就像监听器一样。
这就是我的实现方式:
// Create s3 client and set region and endpoints
AmazonS3Client s3Client = new AmazonS3Client(IdentityManager.getDefaultIdentityManager().getCredentialsProvider(), new ClientConfiguration());
s3Client.setRegion(Region.getRegion(Regions.US_EAST_1));
// Create a tranfer utility and attach to s3 client
TransferUtility transferUtility = TransferUtility.builder().s3Client(s3Client).context(getActivity()).build();
// String key for the file to download
String key = "ExampleVideo.mp4";
// Location to download files from S3
File file = new File(Environment.getExternalStorageDirectory().toString() + "/" + key);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// Bucket that contains the file to download
String bucket = "My_Bucket";
// Create a tranfer observer to download
TransferObserver observer = transferUtility.download(bucket,key,file);
// Attach listener to the download to listen for changes in the the download process
observer.setTransferListener(new TransferListener() {
@Override
public void onStateChanged(int id, TransferState state) {
}
@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
}
@Override
public void onError(int id, Exception ex) {
}
});
感谢您的帮助
最佳答案
适用于 Android 的 AWS 开发工具包 2.6.6
版本包含修复进度更新问题的修复程序。升级到此版本应该可以缓解该问题。
除此之外,您还可以调整 AmazonS3Client
的 notificationThreshold
API 来更改通知进度的时间间隔。
关于android - AWS s3 TransferListener 未在 Android 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48210469/