Problem code:
Bitmap bmp = BitmapFactory.decodeResource(resources, R.drawable.ic_image);
R.rawable. Ic_image. This code works fine on 4.4, but null Pointers will appear on 5.0 and older systems because the original method does not convert a vector to a bitmap. PNGS are produced from a vector, whereas the 4.4 system runs this code using PNG resources. This is why errors are reported above 5.0 and not 4.4.
Solution:
private static Bitmap getBitmap(Context context, int vectorDrawableId) {
Bitmap bitmap = null;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
Drawable vectorDrawable = context.getDrawable(vectorDrawableId);
bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
} else {
bitmap = BitmapFactory.decodeResource(context.getResources(), vectorDrawableId);
}
return bitmap;
}
Copy the code