微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从Android上传到Amazon S3的文件缓慢

我已经按照亚马逊指南,将文件上传到Amazon S3,我注意到这太慢了.上传一个简单的png文件约20kb需要大约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以给其他线程运行时间.

原文地址:https://www.jb51.cc/android/312351.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐