Circularimageview And Api 16: Nullpointerexception Error
I was testing my app on different APIs and it seems to work on API 17 till 23 but I found in API 16, I cannot load my customview. I just want to display a photo in a circle and I h
Solution 1:
Well since your bitmap is null and the drawable isn't the only place the error can be at is within copy(Bitmap, boolean)
.
If you look at the documentation
If the conversion is not supported, or the allocator fails, then this returns NULL.
Try using a smaller image, since this can be a sign of Low Memory. You should also add some guard against the possible null value returned, e.g. just drawing the normal bitmap in that case.
Solution 2:
Use this method to convert your drawable to a bitmap:
publicstatic Bitmap drawableToBitmap(Drawable drawable) {
Bitmapbitmap=null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawablebitmapDrawable= (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvascanvas=newCanvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
Then instead of using in the original code, the following line:
Bitmapb= ((BitmapDrawable) drawable).getBitmap();
Use this instead:
Bitmapb= drawableToBitmap(drawable);
Then it will run without error.
Solution 3:
You can use MLRoundedImageView for circular imageview. MLRoundedImageView.java
publicclassMLRoundedImageViewextendsImageView {
publicMLRoundedImageView(Context context) {
super(context);
}
publicMLRoundedImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
publicMLRoundedImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@OverrideprotectedvoidonDraw(Canvas canvas) {
Drawabledrawable= getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmapb= ((BitmapDrawable) drawable).getBitmap();
Bitmapbitmap= b.copy(Bitmap.Config.ARGB_8888, true);
intw= getWidth(), h = getHeight();
BitmaproundBitmap= getCroppedBitmap(bitmap, w);
canvas.drawBitmap(roundBitmap, 0, 0, null);
}
publicstatic Bitmap getCroppedBitmap(Bitmap bmp, int radius) {
Bitmap sbmp;
if (bmp.getWidth() != radius || bmp.getHeight() != radius) {
floatsmallest= Math.min(bmp.getWidth(), bmp.getHeight());
floatfactor= smallest / radius;
sbmp = Bitmap.createScaledBitmap(bmp, (int)(bmp.getWidth() / factor), (int)(bmp.getHeight() / factor), false);
} else {
sbmp = bmp;
}
Bitmapoutput= Bitmap.createBitmap(radius, radius,
Config.ARGB_8888);
Canvascanvas=newCanvas(output);
finalintcolor=0xffa19774;
finalPaintpaint=newPaint();
finalRectrect=newRect(0, 0, radius, radius);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(radius / 2 + 0.7f,
radius / 2 + 0.7f, radius / 2 + 0.1f, paint);
paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(sbmp, rect, rect, paint);
return output;
}
}
Then use like this in layout
<com.yourproject.MLRoundedImagViewLayoutpropertieshere />
Then in your activity
MLRoundedImagViewimageview= (MLRoundedImagView) findViewbyId(R.id.imageview);
Post a Comment for "Circularimageview And Api 16: Nullpointerexception Error"