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)
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);
}
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;
}
Related
I take a fingerprint from the user and add white background to it using glide. The issue is I need to get the byte array of that image and I can't get it because the Image being hold into the ImageView is a VectorDrawable. I am unable to convert this back to bitmap and thus to a byte array. I tried using it on a dummy png image like following.
Take any dummy PNG Image
firstIV = findViewById(R.id.firstIV);
secondIV = findViewById(R.id.secondIV);
Bitmap currentBmp = BitmapFactory.decodeResource(getResources(), R.drawable.test_image);
Glide.with(this).load(currentBmp).into(new SimpleTarget<Drawable>() {
#Override
public void onResourceReady(Drawable resource, Transition<? super Drawable> glideAnimation) {
final ShapeDrawable background = new ShapeDrawable();
background.getPaint().setColor(Color.WHITE);
final Drawable[] layers = {background, resource};
firstIV.setImageDrawable(new LayerDrawable(layers));
setToAnotherImageView(firstIV.getDrawable().getIntrinsicWidth(), firstIV.getDrawable().getIntrinsicHeight());
}
});
Applied Solution
private void setToAnotherImageView(int width, int height) {
Log.e(TAG, "onCreate: " + width + ":" + height);
Bitmap convertedBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(convertedBmp);
firstIV.draw(canvas);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
convertedBmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Bitmap newBmp = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
secondIV.setImageBitmap(newBmp);
}
Upon doing so my app crashes with an exception saying the vector drawable can not be converted to bitmap. The following is the error:
Originally what I want to achieve
Originally I want the bytearray of the the picture I have stored in my ImageView so I can convert that bytearray to WSQ bytearray and the send it to my server. I am trying to achieve such with an example defined above.
UPDATE:
Just find out that there is a new way to get Bitmap from ImageView:
imageView.drawToBitmap() //draw from the ImageView size
imageView.drawable.toBitmap() //draw from original Drawable size (I guess)
Convert ImageView to ByteArray:
private fun getImageBytes(): ByteArray {
val bitmap = Bitmap.createBitmap(my_profile_imageView.width, my_profile_imageView.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
my_profile_imageView.draw(canvas)
val outputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
return outputStream.toByteArray()
}
However, it's not a good idea to store the image ByetArray in local, you should store the uri or url instead.
My question is a duplicate of How to get a Bitmap from VectorDrawable
However I solved it like this
private void setToAnotherImageView() {
Drawable drawable = firstIV.getDrawable();
Bitmap newBmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBmp);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
secondIV.setImageBitmap(newBmp);
}
I need to change the default cross-icon in ChromeCustomTab Android, I am changing it with back-icon using the code below:
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_arrow_back_white_24dp);
It is working fine with PNGs but not with SVGs.
As per this documentation, we have to maintain size of image according to this documentation.
https://developer.android.com/reference/android/support/customtabs/CustomTabsIntent.html#KEY_ICON
I think it is not working because they are not following the dimensions given in Documentation.
You need to return a valid Bitmap. For a VectorDrawable it is necessary to do something more. You can use these methods:
private static Bitmap bitmapFromDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (drawable instanceof VectorDrawable) {
return bitmapFromVectorDrawable((VectorDrawable) drawable);
}
return ((BitmapDrawable) drawable).getBitmap();
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Bitmap bitmapFromVectorDrawable(VectorDrawable vectorDrawable) {
Bitmap 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);
return bitmap;
}
Then you can use it like:
builder.setCloseButtonIcon(bitmapFromDrawable(this, R.drawable. ic_arrow_back_white_24dp));
Because I need my drawable as bitmap descriptor, I convert my resource to a Drawable then to a Bitmap.
But I want to change its color, So I used the following code found somewhere here:
private static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (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);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
int iColor = Color.parseColor("#006f00");
drawable.setColorFilter(iColor, PorterDuff.Mode.SRC_ATOP);
drawable.draw(canvas);
return bitmap;
}
But the drawable's color remains black.
Can you please tell me why and how to get it working?
This converts a BitmapDrawable to a Bitmap.
Drawable d = ImagesArrayList.get(0);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
And for changing bitmap's color see below link
How to change Bitmap image color in android?
Paint p = new Paint();
ColorFilter filter = new LightingColorFilter(Color.RED, 0);
p.setColorFilter(filter);
canvas.drawBitmap(bitmap, 0, 0, p);
To convert drawable into bitmap use this -
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
You can change bitmap pixels color by this-
int[] pixels = new int[myBitmap.getHeight()*myBitmap.getWidth()];
myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
for (int i=0; i<myBitmap.getWidth()*5; i++)
pixels[i] = Color.BLUE;
myBitmap.setPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
I think the problem could be PorterDuff.Mode.SRC_ATOP, the method you use for apply the filter. I don't know specifically SRC_ATOP but this link explains all of them.
I don't know how your image is composed, but I'd use DST_ATOP instead of SRC_ATOP.
EDIT: I presumed that this code won't return in the case you are analizing, 'cause if it returns the filter obviously is never reached.
if (drawable instanceof BitmapDrawable)
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
return bitmapDrawable.getBitmap();
}
}
I'm looking for a way on how can I convert a TransitionDrawable to Drawable or To bitmap to be able to save the Image.
I have been looking for a while and I tried a lot methods and no clue.
here a code what I have tried :
TransitionDrawable drawable = (TransitionDrawable) profil.getDrawable();
Drawable drawalb = drawable.mutate();
final Bitmap bitmap = ((BitmapDrawable) drawalb).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] image = stream.toByteArray();
I wantn't able to do that, but I have found another clue, I was able to get the bitmap directly before converting it to TransitionDrawable.
A transitionDrawable is an array of drawable (Defnition).
so;
1.Get the drawable you want, depending on its index
2.covert it to a bitmap (As cited here by Andre)
TransitionDrawable Tdrawable =(TransitionDrawable) imageView.getDrawable();
Drawable mDrawable = Tdrawable.getDrawable;
bitmap = drawableToBitmap(mDrawable);
public static Bitmap drawableToBitmap (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (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);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
There's say some ImageView object. I want to read bits/raw data of this object as InputStream. How to do that?
First get background image of the ImageView as an object of Drawable:
iv.getBackground();
Then convert Drawable image into Bitmap using
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
Now use ByteArrayOutputStream to get the Bitmap into a Stream and get bytearray[]; then
convert the bytearray into a ByteArrayInputStream.
You can use the following code to get InputStream from ImageView.
Full Source code
ImageView iv = (ImageView) findViewById(R.id.splashImageView);
Drawable d = iv.getBackground();
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bitmap = bitDw.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte = stream.toByteArray();
System.out.println("........length......" + imageInByte);
ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte);
Thanks
Deepak
These methods below are useful because they work with any kind of Drawable (not only BitmapDrawable). If you want to use drawing cache as in David Caunt's suggestion, consider using bitmapToInputStream instead of bitmap.compress, because it should be faster.
public static Bitmap drawableToBitmap (Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
public static InputStream bitmapToInputStream(Bitmap bitmap) {
int size = bitmap.getHeight() * bitmap.getRowBytes();
ByteBuffer buffer = ByteBuffer.allocate(size);
bitmap.copyPixelsToBuffer(buffer);
return new ByteArrayInputStream(buffer.array());
}
You can use the drawing cache to retrieve a Bitmap representation of any View class.
view.setDrawingCacheEnabled(true);
Bitmap b = view.getDrawingCache();
Then you can write the bitmap to an OutputStream, for example:
b.compress(CompressFormat.JPEG, 80, new FileOutputStream("/view.jpg"));
In your case I think you can use a ByteArrayOutputStream to get a byte[] from which you can create an InputStream. The code would be something like this:
ByteArrayOutputStream os = new ByteArrayOutputStream(b.getByteCount());
b.compress(CompressFormat.JPEG, 80, os);
byte[] bytes = os.toByteArray();
You might be looking for this:
openRawResource