关于android:在位图中转换imageview时,位图为null

Bitmap is null when convert imageview in bitmap

我需要在图像中放置效果模糊,但是将imageview转换为位图时会显示错误(NullPointerException)。

查看代码:

1
2
3
// mImages is a list of string (links http of images in web)      
Picasso.with(mContext).load(mImages.get(position)).fit().centerInside().into(imageView);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();

使用Picasso回调:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Picasso.with(this)
    .load(mImages.get(position))
    .fit()
    .centerInside()
    .into(imageView, new Callback() {
        @Override
        public void onSuccess() {
            // Drawable is ready
            Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        }

        @Override
        public void onError() {

        }
    });

确保您的ImageView的widthheight没有由WRAP_CONTENT定义。

这是因为fit()在测量ImageView时需要其尺寸。

如果ImageView的宽度和高度由WRAP_CONTENT定义,则其方法getMeasuredWidth()getMeasuredHeight()将返回0,并且您将看不到图像。


您确定imageView!= null吗?如果条件

,将其package在里面

1
2
3
4
5
6
if(imageView != null){
// write your code here
}else{
//log something like the following
Log.e("imageView","NULL");
}

您确定它是内置的吗?尝试下一个代码

1
2
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();