Glide载入图片后保存图片 [英] Save image once Glide has loaded it
问题描述
我使用Glide
加载图像,例如:
I load images using Glide
like such:
Glide.with(getContext()).load(apdInfo.url)
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(apd_image);
并希望保存这样加载的位图:
And wish to save the Bitmap loaded like such:
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
,它将无法正常工作.
The issue is that if I call saveImage
before Glide
has loaded my picture, it won't work.
如何等待或按Glide
加载它?请注意,使用Glide
的监听器无效.
How may I wait or Glide
to load it? Please note that using Glide
's listeners did not work.
编辑:
我现在使用以下侦听器:
I now use the following listeners:
.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
方法保存它.
Use the listener so that when the image is fetched and ready to be used, you can save it using the saveImage
method of yours.
执行此操作:
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
获取图像.
Call the saveImage
method inside onResourceReady() callback and use the GlideDrawable
resource. I would suggest using the GlideDrawable
rather than getting the image using Drawing Cache
.
这篇关于Glide载入图片后保存图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!