使用香草放大
downloadFile() 将在后台线程上执行其工作。只需使用 standard approaches 之一从回调返回主线程:
Handler handler = new Handler(context.getMainLooper());
File file = new File(context.getFilesDir() + "/download.txt");
Amplify.Storage.downloadFile(
"ExampleKey", file,
result -> {
handler.post(() -> {
Log.i("MyAmplifyApp", "Successfully downloaded: " + result.getFile().getName());
});
},
error -> Log.e("MyAmplifyApp", "Download Failure", error)
);
使用 Rx 绑定
但就个人而言,我会使用 Rx 绑定。 The official documentation 包括用于 Rx API 的 sn-ps。这是一个更量身定制的示例:
File file = new File(context.getFilesDir() + "/download.txt");
RxAmplify.Storage.downloadFile("ExampleKey", file)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(result -> {
Log.i("RxExample", "Download OK.");
}, failure -> {
Log.e("RxExample", "Failed.", failure);
});
并行运行多个下载
通过调用RxAmplify.Storage.downloadFile("key", local) 构建Singles 的集合。然后,使用Single.mergeArray(...) 将它们全部组合起来。订阅它,方法同上。
RxStorageCategoryBehavior storage = RxAmplify.Storage;
Single
.mergeArray(
storage.downloadFile("one", localOne)
.observeResult(),
storage.downloadFile("two", localTwo)
.observeResult()
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(/* args ... */);
报告错误
您提到您遇到了意外的异常。如果是这样,请提交错误here,我会修复它。