getDrawable()给出空对象,而试图从ImageView的位图 [英] getDrawable() gives null object while trying to get bitmap from imageview

查看:4285
本文介绍了getDrawable()给出空对象,而试图从ImageView的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ImageView的下滑,我想从ImageView的位图 -

I am using glide for imageview and I want to get bitmap from that 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

帮助我。

推荐答案

滑翔加载图像不同步,所以你对位的测试,启动装载作业之后将返回null,因为图像没有被加载。

Glide loads the image asynchronously, so your test on the bitmap, right after initiating that loading operation will return null, as the image has not yet been loaded.

要知道,当你的形象确实已经加载,你可能在你的滑翔要求,例如设置一个监听器:

To know when your image has indeed been loaded, you may set a listener in your Glide request, e.g.:

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);

这篇关于getDrawable()给出空对象,而试图从ImageView的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆