3

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 shows me an error class cast exception unable to convert vector drawable to bitmap drawable.

 Resources res = getResources();
   Drawable drawable = res.getDrawable(R.drawable.ic_motorcycle_black);
    if (drawable != null) {
        drawable.setColorFilter(0xffff0000, PorterDuff.Mode.MULTIPLY);
    }
    Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    final byte[] bike = stream.toByteArray();

Error:

  Caused by: java.lang.ClassCastException: android.graphics.drawable.VectorDrawable cannot be cast to android.graphics.drawable.BitmapDrawable
                                                                  at codingtown.coconut.otherexpense.activity.AddNewExpenseCategoryActivity.intialize(AddNewExpenseCategoryActivity.java:82)
                                                                  at codingtown.coconut.otherexpense.activity.AddNewExpenseCategoryActivity.onCreate(AddNewExpenseCategoryActivity.java:67)
                                                                  at android.app.Activity.performCreate(Activity.java:6092)
                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
Ramesh R
  • 300
  • 4
  • 18
  • http://stackoverflow.com/questions/38702131/auto-mirroring-for-rtl-layout-doesnt-work-in-android-versions-below-6-0/41851233#41851233 – amorenew Jan 25 '17 at 12:10

2 Answers2

0

You can try this. It works fine with me.

private BitmapDescriptor bitmapDescriptorFromVector(Context context, @DrawableRes  int vectorDrawableResourceId) {
    Drawable background = ContextCompat.getDrawable(context, R.drawable.ic_map_pin_filled_blue_48dp);
    background.setBounds(0, 0, background.getIntrinsicWidth(), background.getIntrinsicHeight());
    Drawable vectorDrawable = ContextCompat.getDrawable(context, vectorDrawableResourceId);
    vectorDrawable.setBounds(40, 20, vectorDrawable.getIntrinsicWidth() + 40, vectorDrawable.getIntrinsicHeight() + 20);
    Bitmap bitmap = Bitmap.createBitmap(background.getIntrinsicWidth(), background.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    background.draw(canvas);
    vectorDrawable.draw(canvas);
    return BitmapDescriptorFactory.fromBitmap(bitmap);
}
Chivorn
  • 2,203
  • 2
  • 21
  • 37
-1

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);

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
} catch (OutOfMemoryError e) {
    // Handle the error
    return null;
}
Daniel Beck
  • 20,653
  • 5
  • 38
  • 53