gpt4 book ai didi

Android - 如何从 s3 下载图像?

转载 作者:行者123 更新时间:2023-11-29 14:45:04 39 4
gpt4 key购买 nike

我正在阅读 s3 android 指南,我真的很困惑如何下载我的文件。

他们提供了这段代码:

TransferObserver observer = transferUtility.download(
MY_BUCKET, /* The bucket to download from */
OBJECT_KEY, /* The key for the object to download */
MY_FILE /* The file to download the object to */
);

那么什么是 MY_FILE?我是否应该创建一个本地空文件对象并将其提供给 transferUtility 下载函数并将该空文件填充到一个下载?

并且,当我完成获取文件(特别是图像)时,我如何使用 glide 或 Picasso 将该文件上传到 imageView?

我不确定如何使用 TransferObserver 对象。

希望有人能提供一个工作示例,拜托!

干杯!

最佳答案

虽然我回答这个问题已经很晚了。希望这对陷入此问题的人有所帮助。

您不需要公开存储桶。您可以通过 Glide 直接显示图像。这是我通过 Glide 从 amazon s3 bucket 加载图像的 repo。

https://github.com/jontyankit/Glide-Amazon-Image-Load

您需要覆盖 GlideModule 并注册我们的组件

public class CustomGlideModule implements GlideModule {

@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
}

@Override
public void registerComponents(Context context, Glide glide) {
glide.register(ImageModel.class, InputStream.class, new ImageLoader.Factory());
}
}

制作自定义 ModelLoader 类。该类根据上述模型而不是URL获取图片

 public class ImageLoader implements ModelLoader<ImageModel, InputStream> {

private final ModelCache<ImageModel, ImageModel> mModelCache;

public ImageLoader(ModelCache<ImageModel, ImageModel> mModelCache) {
this.mModelCache = mModelCache;
}

@Override
public DataFetcher<InputStream> getResourceFetcher(ImageModel model, int width, int height) {
ImageModel imageModel = model;
if (mModelCache != null) {
imageModel = mModelCache.get(model, 0, 0);
if (imageModel == null) {
mModelCache.put(model, 0, 0, model);
imageModel = model;
}
}
return new ImageFetcher(imageModel);
}

public static class Factory implements ModelLoaderFactory<ImageModel, InputStream> {

private final ModelCache<ImageModel, ImageModel> mModelCache = new ModelCache<>(500);

@Override
public ModelLoader<ImageModel, InputStream> build(Context context, GenericLoaderFactory factories) {
return new ImageLoader(mModelCache);
}

@Override
public void teardown() {

}
}

}

最后创建自定义类 DataFetcher。 public InputStream loadData(Priority priority) 是从亚马逊下载图片的方法。

public class ImageFetcher implements DataFetcher<InputStream> {

private final ImageModel imageModel;
private InputStream mInputStream;
boolean downloadComplete = false;
int transferId = 0;

public ImageFetcher(ImageModel imageModel) {
this.imageModel = imageModel;
}

@Override
public InputStream loadData(Priority priority) throws Exception {
return fetchStream(imageModel);
}

private InputStream fetchStream(final ImageModel imageModel) {
TransferUtility transferUtility = AmazonClient.getClient().getTransferUtility();
TransferObserver bolomessages = transferUtility.download(imageModel.getBucketName(), imageModel.getId(), new File(imageModel.getLocalPath()));
transferId = bolomessages.getId();

bolomessages.setTransferListener(new TransferListener() {

@Override
public void onStateChanged(int id, TransferState state) {
Log.wtf("AWSS3", "onStateChanged = " + state);
if (state == TransferState.COMPLETED) {
File initialFile = new File(imageModel.getLocalPath());
try {
mInputStream = new FileInputStream(initialFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
downloadComplete = true;
}
}

@Override
public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
}

@Override
public void onError(int id, Exception ex) {
// do something
Log.wtf("AWSS3", "onError");
ex.printStackTrace();
downloadComplete = true;
}
});
while (!downloadComplete){}
return mInputStream;
}

@Override
public void cleanup() {
if (mInputStream != null) {
try {
mInputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
mInputStream = null;
}
}
}

@Override
public String getId() {
return imageModel.getId();
}

@Override
public void cancel() {
AmazonClient.getClient().getTransferUtility().cancel(transferId);
}
}

关于Android - 如何从 s3 下载图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41994264/

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