2

I need put effect blur in image, but when convert imageview into bitmap it is showing an error (NullPointerException).

See the code:

// 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();
aneurinc
  • 1,238
  • 12
  • 20
Pablo SN
  • 33
  • 4

2 Answers2

2

Make use of Picasso callback:

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() {

        }
    });

Make sure your ImageView's width and height are NOT defined by WRAP_CONTENT.

This is because fit() requires the size of the ImageView when it is measured.

If your ImageView has its width and height defined by WRAP_CONTENT, its methods getMeasuredWidth() and getMeasuredHeight() will return 0 and you will not see the image.

aneurinc
  • 1,238
  • 12
  • 20
0

Are you sure imageView != null ? wrap it inside if condition

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

Are you sure it's built? Try next code

imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
Seif Mostafa
  • 58
  • 10
  • I entered the verification as you suggested. The imageview is not null. I put the suggested code and it is still giving null in the bitmap – Pablo SN Mar 18 '17 at 21:44
  • Actually I managed to solve with the solution passed by @User123, but thanks for the help – Pablo SN Mar 19 '17 at 00:50