Glide 加载图像后保存图像
Save image once Glide has loaded it
我像这样使用 Glide
加载图像:
Glide.with(getContext()).load(apdInfo.url)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(apd_image);
并希望像这样保存加载的位图:
private void saveImage() {
final ImageView apd_image = (ImageView) view.findViewById(R.id.apd_image);
final InternalFileHandler IFH = new InternalFileHandler(getContext());
apd_image.setDrawingCacheEnabled(true);
apd_image.buildDrawingCache(true);
Bitmap bitmap = apd_image.getDrawingCache();
// Simply saves the bitmap in the filesystem
IFH.savePicture("APD", bitmap, getContext());
apd_image.destroyDrawingCache();
}
问题是,如果我在 Glide
加载我的图片之前调用 saveImage
,它将不起作用。
我该如何等待或Glide
加载它?请注意,使用 Glide
的侦听器无效。
编辑:
我现在使用以下侦听器:
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
//handle the exception.
return true;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
// onResourceReady is called twice for some reason, this is my work around for now
if (first) {
GlideBitmapDrawable glideBitmapDrawable = (GlideBitmapDrawable) resource;
// Convert to Bitmap
Bitmap bm = glideBitmapDrawable.getBitmap();
saveImage(bm);
first = false;
} else {
first = true;
}
return true;
}
})
使用侦听器,以便在获取图像并准备好使用时,您可以使用您的 saveImage
方法保存它。
这样做:
Glide.with(getContext()).load(apdInfo.url)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable glideDrawable, GlideAnimation<? super GlideDrawable> glideAnimation) {
apd_image.setImageDrawable(glideDrawable);
apd_image.setDrawingCacheEnabled(true);
saveImage();
}});
在 onResourceReady() 回调中调用 saveImage
方法并使用 GlideDrawable
资源。我建议使用 GlideDrawable
而不是使用 Drawing Cache
.
获取图像
我像这样使用 Glide
加载图像:
Glide.with(getContext()).load(apdInfo.url)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(apd_image);
并希望像这样保存加载的位图:
private void saveImage() {
final ImageView apd_image = (ImageView) view.findViewById(R.id.apd_image);
final InternalFileHandler IFH = new InternalFileHandler(getContext());
apd_image.setDrawingCacheEnabled(true);
apd_image.buildDrawingCache(true);
Bitmap bitmap = apd_image.getDrawingCache();
// Simply saves the bitmap in the filesystem
IFH.savePicture("APD", bitmap, getContext());
apd_image.destroyDrawingCache();
}
问题是,如果我在 Glide
加载我的图片之前调用 saveImage
,它将不起作用。
我该如何等待或Glide
加载它?请注意,使用 Glide
的侦听器无效。
编辑:
我现在使用以下侦听器:
.listener(new RequestListener<String, GlideDrawable>() {
@Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
//handle the exception.
return true;
}
@Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
// onResourceReady is called twice for some reason, this is my work around for now
if (first) {
GlideBitmapDrawable glideBitmapDrawable = (GlideBitmapDrawable) resource;
// Convert to Bitmap
Bitmap bm = glideBitmapDrawable.getBitmap();
saveImage(bm);
first = false;
} else {
first = true;
}
return true;
}
})
使用侦听器,以便在获取图像并准备好使用时,您可以使用您的 saveImage
方法保存它。
这样做:
Glide.with(getContext()).load(apdInfo.url)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable glideDrawable, GlideAnimation<? super GlideDrawable> glideAnimation) {
apd_image.setImageDrawable(glideDrawable);
apd_image.setDrawingCacheEnabled(true);
saveImage();
}});
在 onResourceReady() 回调中调用 saveImage
方法并使用 GlideDrawable
资源。我建议使用 GlideDrawable
而不是使用 Drawing Cache
.