我正在阅读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下载功能,并将该空文件填入一个下载文件?
而且,当我完成文件后,(特别是图像)我如何使用滑翔或毕加索将该文件上传到imageView?
我不确定如何使用TransferObserver对象。
希望有人可以提供一个有效的例子!
喝彩!
答案 0 :(得分:4)
虽然我很晚才回答这个问题。希望这可以帮助那些陷入这个问题的人。
您无需公开存储桶。您可以通过Glide直接显示图像。这是我通过Glide从亚马逊s3桶装载图像的回购。
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(优先级优先级)是从亚马逊下载图片的方法。
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);
}
}
答案 1 :(得分:2)
因此,我可以使用Glide
或picasso
使用s3存储桶中图像的url
加载图像。但是你必须公开这个桶。
以下是您上传图片的方式:
Glide.with(getActivity().getBaseContext()).load("IMAGE URL FROM S3").centerCrop().into(cardImage);
感谢@KNeerajLal,您可以在这里公开宣传。 这里: