我正在使用 glide 进行 imageview,我想从该 imageview 获取位图 -
ImageView imageView = (ImageView) findViewById(R.id.dp);
Glide.with(this).load("http://graph.facebook.com/1615242245409408/picture?type=large").into(imageView);
Bitmap fbbitmap2 = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
但它给了
java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' on a null object reference
帮帮我。
Glide 异步加载图像,因此在启动加载操作后对位图进行的测试将返回 null,因为图像尚未加载。
要知道您的图像何时确实已加载,您可以在 Glide 请求中设置一个侦听器,例如:
Glide.with(this)
.load("http://graph.facebook.com/1615242245409408/picture?type=large")
.listener(new RequestListener<Uri, GlideDrawable>() {
@Override
public boolean onException(Exception e, Uri model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, Uri model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
// drawable is in resource variable
// if you really need to access the bitmap, you could access it using ((GlideBitmapDrawable) resource).getBitmap()
return false;
}
})
.into(imageView);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)