Unable To Convert Vector Drawable To Bitmap Drawable In Android
I am trying to convert a bitmap to byte array in which i have taken an vector drawable image to bitmap and then i have converted it to byte array but when i open the application it
Solution 1:
You can try this. It works fine with me.
private BitmapDescriptor bitmapDescriptorFromVector(Context context, @DrawableResint vectorDrawableResourceId) {
Drawablebackground= ContextCompat.getDrawable(context, R.drawable.ic_map_pin_filled_blue_48dp);
background.setBounds(0, 0, background.getIntrinsicWidth(), background.getIntrinsicHeight());
DrawablevectorDrawable= ContextCompat.getDrawable(context, vectorDrawableResourceId);
vectorDrawable.setBounds(40, 20, vectorDrawable.getIntrinsicWidth() + 40, vectorDrawable.getIntrinsicHeight() + 20);
Bitmapbitmap= Bitmap.createBitmap(background.getIntrinsicWidth(), background.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvascanvas=newCanvas(bitmap);
background.draw(canvas);
vectorDrawable.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
Solution 2:
You cannot cast VectorDrawable to BitmapDrawable. They don't have a parent-child relationship. They both are direct subclasses of Drawable class.
to get a bitmap from drawable, you will need to create a Bitmap from the drawable metadata.
Probably something like this in a separate method,
try {
Bitmap bitmap;
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);
Canvascanvas=newCanvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
} catch (OutOfMemoryError e) {
// Handle the errorreturnnull;
}
Post a Comment for "Unable To Convert Vector Drawable To Bitmap Drawable In Android"