gpt4 book ai didi

android - 从 Android 缓慢上传文件到 Amazon S3

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:58 28 4
gpt4 key购买 nike

我已经按照 Amazon 指南将文件上传到 Amazon S3,但我注意到它太慢了。上传一个大约 20kb 的简单 png 文件大约需要 10 秒。

一开始以为是线程的问题,实现了AsyncTask上传图片,但问题依旧。以下是用于上传图像的代码。

private class UploadFileTask extends AsyncTask<String, Integer, String> {
String remotePath;
String remoteFileName;
File file;
Context context;
S3UploadInterface listener;


public UploadFileTask(Context context,String remotePath,String remoteFileName, File file, S3UploadInterface listener){
this.context=context;
this.remotePath=remotePath;
this.remoteFileName=remoteFileName;
this.file=file;
this.listener=listener;
}

protected String doInBackground(String... params) {
credentialsProvider = new CognitoCachingCredentialsProvider(context,
"MY_PRIVATE_CREDENTIAL",
Regions.US_EAST_1);
TransferManager transferManager = new TransferManager(credentialsProvider);
Upload upload = transferManager.upload(remotePath, remoteFileName, file);
TransferProgress transferred = upload.getProgress();
while (!upload.isDone()) {
try {
publishProgress((int) transferred.getPercentTransferred());
} catch (Exception e) {
listener.uploadFailed(e);
}
}
return "uploaded";
}

protected void onProgressUpdate(Integer... progress) {
if (listener!=null)
listener.currentUploadProgress(progress[0]);
}

protected void onPostExecute(String result) {
if (listener!=null)
listener.uploadCompleted();
}
}

有解决这个问题的办法吗?谢谢:)

最佳答案

这会影响你的 CPU

    while (!upload.isDone()) {
try {
publishProgress((int) transferred.getPercentTransferred());
} catch (Exception e) {
listener.uploadFailed(e);
}
}

尝试添加 Thread.sleep 以让其他线程有时间运行。

关于android - 从 Android 缓慢上传文件到 Amazon S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31202681/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com