首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Android上使用AWS S3 PersistableDownload

如何在Android上使用AWS S3 PersistableDownload
EN

Stack Overflow用户
提问于 2014-09-22 15:01:39
回答 1查看 303关注 0票数 0

有人能在Android上使用PersistableDownload吗?当应用程序崩溃时,我一直试图用它恢复下载,但到目前为止还没有成功。我认为我没有正确地理解序列化/反序列化的概念。到目前为止,我得到的代码如下:

代码语言:javascript
复制
AmazonS3Client s3Client = getAmazonS3Client(Regions.SA_EAST_1);
TransferManager tx = new TransferManager(s3Client);

String bucket = "MyBucket";
String key = "IMG_20140915_132548.jpg";
String[] parts = key.split("/");
String fileName = parts[parts.length - 1];

final String full_path = "/storage/sdcard0/" + fileName;
File file = new File(full_path);
FileInputStream fis = null;
if(file.exists()) {
    try {
        fis = new FileInputStream(file);
        PersistableDownload persistableUpload = PersistableTransfer.deserializeFrom(fis); 
        Download meuDown = tx.resumeDownload(persistableUpload);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
}
else {
    GetObjectRequest getRequest = new GetObjectRequest(bucket, "IMG_20140915_132548.jpg");
    Download download = tx.download(getRequest, file, new S3ProgressListener() {
        @Override
        public void progressChanged(ProgressEvent arg0) {
            long transferred = arg0.getBytesTransferred();
            Log.d("AWS3", "" + transferred);
        }
        @Override
        public void onPersistableTransfer(PersistableTransfer arg0) {
            Log.d("AWS3", "Writing to file");
            File f = new File("/storage/sdcard0/resume-upload");
                FileOutputStream fos;
            try {
                if (f.exists() == false) 
                    f.createNewFile();
                fos = new FileOutputStream(f);
                arg0.serialize(fos);
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

我注意到onPersistableTransfer方法只调用一次,所以我不知道所有接收到的字节是如何序列化到磁盘的。

关于如何让PersistableDownload工作,有什么建议吗?我使用的是SDK2.1,带有真正的手机(Android4.4.4)和Eclipse。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-24 17:26:23

从上面的代码中,我看到您正在传递部分下载的图像文件来恢复下载过程。

代码语言:javascript
复制
if(file.exists()) {
    try {
        fis = new FileInputStream(file);
        PersistableDownload persistableUpload = PersistableTransfer.deserializeFrom(fis); 
        Download meuDown = tx.resumeDownload(persistableUpload);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
} 

这里的文件是指部分下载的图像文件。您将需要传递文件“/storage/sdcard0 0/”才能继续上传。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25977249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档