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

前端之家收集整理的这篇文章主要介绍了从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.f2er.com/android/312351.html

猜你在找的Android相关文章